Reputation: 673
I want to hide all .php
extensions from the URL, and if there are GET variables, I need to hide them too.
Ex.:
www.mysite.com/panel/employees.php
to www.mysite.com/panel/employees
www.mysite.com/panel/employees.php?c=1
to www.mysite.com/panel/employees/1
www.mysite.com/adm/tables.php
to www.mysite.com/adm/tables
www.mysite.com/adm/tables.php?c=3
to www.mysite.com/adm/tables/3
So I came up with the following code (with some research)
RewriteEngine On
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
It works fine, however, it hides only the extension and not the variable (Ex.: www.mysite.com/panel/employees.php?c=1
to www.mysite.com/panel/employees?c=1
).
I tried to make changes to the above code, however, since I'm new to .htaccess
, I did not succeed. So if anyone can help me explain how to do it, I would be very grateful.
Upvotes: 1
Views: 115
Reputation: 785128
You need additional set of rules to handle query parameter:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+([^.]+)\.php\?c=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=301,L,NE]
RewriteRule ^(.+)\.php$ /$1 [R=301,NC,L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [END]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+)/([\w-]+)/?$ $1.php?c=$2 [END,QSA]
Upvotes: 1