Igor
Igor

Reputation: 673

Hide all php extensions and GET variables (if exists)

I want to hide all .php extensions from the URL, and if there are GET variables, I need to hide them too.

Ex.:

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

Answers (1)

anubhava
anubhava

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

Related Questions