Replace .php in URL with / using .htaccess

Predator

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/

anubhava

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]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

replace url with htaccess

From Dev

Replace a part of url with htaccess RewriteRule

From Dev

Replace .php in URL with / using .htaccess

From Dev

replace space by - using htaccess

From Dev

how to replace % in url with htaccess

From Dev

Friendly URL using .htaccess and PHP "include"

From Dev

Change URL .php extension using .htaccess

From Dev

Rewrite url using .htaccess in PHP

From Dev

URL rewriting in PHP using htaccess file

From Dev

Url redirection issue using .htaccess and php

From Dev

How to find and replace a specific word from a URL using htaccess

From Dev

Replace URL parameter separator with .htaccess

From Dev

How to replace a parameter in all of my url's using .htaccess?

From Dev

Change PHP URL with .htaccess

From Dev

how to replace % in url with htaccess

From Dev

Replace the page from a url using php

From Dev

URL Rewriting in php CodeIgniter using .htaccess

From Dev

Friendly URL using .htaccess and PHP "include"

From Dev

Replace URL in PHP code using HTTP Class

From Dev

Rewrite url using .htaccess in php not working

From Dev

Replace portions of a url path and query string using .htaccess and Regex?

From Dev

Hide page name in url using htaccess in php

From Dev

Create tree stucture url in php using .htaccess

From Dev

URL rewrite with PHP and .htaccess

From Dev

URL Rewrite PHP .htaccess

From Dev

URL .htaccess in php

From Dev

Replace text in a url using PHP

From Dev

Specific url to work with and without .php using .htaccess

From Dev

.htaccess: Replace part of url for a domain