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]
Solution 1:
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]
Solution 2:
ByTheWay don't forget to enable mod rewrite,in apache on ubuntu
sudo a2enmod rewrite
and then restart apache2
sudo /etc/init.d/apache2 restart
edit: it is an old question that helped me but I lost hours testing my apache2.conf file on one side and .htaccess file on the other side and still no light.
Solution 3:
To rewrite all requests to /index.php
,you can use :
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule ^(.+)$ /index.php?url=$1 [NC,L]
The RewriteCondition RewriteCond %{REQUEST_URI} !^/index.php$
is important as it excludes the rewrite destination we are rewriting to and prevents infinite looping error.