.htaccess mod_rewrite on multiple PHP files issue-ThrowExceptions

Exception or error:

I’m trying to beautify URL’s for SEO from http://example.net/dashboard.php?action=news to http://example.net/news
And it’s working with this code

RewriteEngine On
RewriteCond %{THE_REQUEST} \/+dashboard\.php\?([^\&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ dashboard.php?action=$1 [L,QSA]

But I want to edit more PHP files, for example, http://example.net/user.php?action=profile to http://example.net/profile

If I use this code again in the same .htaccess file

RewriteEngine On
RewriteCond %{THE_REQUEST} \/+user\.php\?([^\&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ user.php?action=$1 [L,QSA]

It won’t work, only dashboard.php one is working properly.
Can you please help me with this?

Thank you in advance.

How to solve:

Your current Rewriterules are to general.

[^/]+

matches everything which has no “/”, else its matching everything.
So basicly your first rule

RewriteRule ^([^/]+)/?$ dashboard.php?action=$1 [L,QSA]

matches everytime. profile/ will always redirected to dashboard.php?acton=profile.

Your .htaccess should look like:

RewriteEngine On
RewriteCond %{THE_REQUEST} \/+dashboard\.php\?([^\&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(news)/?$ dashboard.php?action=$1 [L,QSA]

RewriteCond %{THE_REQUEST} \/+user\.php\?([^\&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(profile)/?$ user.php?action=$1 [L,QSA]

As you can see i switched

[^/]+

with more explicit regex.

Leave a Reply

Your email address will not be published. Required fields are marked *