Reputation: 611
I want to redirect all users from mysite.com/username
to mysite.com/user.php?user=username
I am now using the following code:
RewriteEngine on
RewriteBase /
RewriteRule ([^/.]+)/ user.php?user=$1 [L]
RewriteRule ([^/.]+) user.php?user=$1 [L]
It works, but it is also redirecting directory paths into user.php
(Ex. mysite.com/css/style.css
-> mysite.com/user.php?user=css/style.css
).
What should I do? Any suggestions?
Upvotes: 2
Views: 79
Reputation: 785581
You just need to skip requests for real files and directories like this:
RewriteEngine on
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^/.]+)/?$ user.php?user=$1 [L,QSA]
Upvotes: 1
Reputation: 12089
Add a RewriteCond
to exclude requests ending in .css
:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !\.css$
RewriteRule ([^/.]+)/? user.php?user=$1 [L]
I combined your 2 rules into one using a question mark ?
to make the trailing forward slash optional.
If you so desire you can also exclude other "helper" files. And it might be helpful to also exclude re-rewriting the request for resource user.php:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpe?g|gif)$
RewriteCond %{REQUEST_URI} !user.php$
RewriteRule ([^/.]+)/? user.php?user=$1 [L]
Upvotes: 0