Yas
Yas

Reputation: 465

Replace .php in URL with / using .htaccess

I've been searching all day for a solution to replace .php in the URL with a / using .htaccess. Most of the solutions didn't work for me at all (didn't rewrite the URL, even just to remove .php) until I found this beautiful solution on SO.

https://stackoverflow.com/a/11084526/1724376

Now my issue is that it only removes the .php but does not replace it with a "/". I've tried many things with no luck but I don't know much about htaccess and rewrite conditions, etc. I'm really hoping someone here can help me.

Just so I don't get down-voted for not having tried anything, here's one that I tried but it didn't rewrite the URL at all.

RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php

Help will be truly appreciated.

EDIT: To clarify, I want www.mysite.com/contact.php to show up as www.mysite.com/contact/

Upvotes: 3

Views: 1276

Answers (1)

anubhava
anubhava

Reputation: 785256

Have your rule like this:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]

RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

Upvotes: 1

Related Questions