jdne
jdne

Reputation: 23

.htaccess take .php off, leading to wrong directory

So, I'm trying to take .php from my URLs and make it so its page and not page.php however I'm having problems because as soon as you go to a directory and a page it puts you to the main directory... So example if its /example/page.php it will take you to /page and not /example/page

This is the .htaccess I've tried.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

Upvotes: 1

Views: 23

Answers (1)

anubhava
anubhava

Reputation: 785631

Replace all of your rules with this:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=301,NE]

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Upvotes: 1

Related Questions