Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect all traffic to index.php using mod_rewrite

I'm trying to build a url shortener, and I want to be able to take any characters immediately after the domain and have them passed as a variable url. So for example

  • http://google.com/asdf

would become

  • http://www.google.com/?url=asdf.

Here's what I have for mod_rewrite right now, but I keep getting a 400 Bad Request:

RewriteEngine on  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^(.*) index.php?url=$1 [L,QSA]  
like image 335
bswinnerton Avatar asked Dec 21 '11 20:12

bswinnerton


People also ask

What is RewriteCond and RewriteRule?

There are two main directive of this module: RewriteCond & RewriteRule . RewriteRule is used to rewrite the url as the name signifies if all the conditions defined in RewriteCond are matching. One or more RewriteCond can precede a RewriteRule directive.

What is Http_host in htaccess?

There is no such Apache server variable HTTPS_HOST , only HTTP_HOST . If HTTPS_HOST is set on your server then it's specific to your server. The HTTP_HOST server variable contains the value of the Host HTTP request header (ie. the hostname), this is irrespective of the protocol used (HTTP or HTTPS).

What is the difference between rewrite and redirect?

Simply put, a redirect is a client-side request to have the web browser go to another URL. This means that the URL that you see in the browser will update to the new URL. A rewrite is a server-side rewrite of the URL before it's fully processed by IIS.

What does RewriteBase mean?

RewriteBase allows you to adjust the path that mod_rewrite automatically prefixes to the result of a RewriteRule . A rewrite within the context of . htaccess is done relative to the directory containing that . htaccess file.


1 Answers

Try replacing ^(.*) with ^(.*)$

RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]

Edit: Try replacing index.php with /index.php

RewriteRule ^(.*)$ /index.php?url=$1 [L,QSA]
like image 175
Rocket Hazmat Avatar answered Oct 20 '22 22:10

Rocket Hazmat