xpredo
xpredo

Reputation: 1523

Removing .php extension in URLs except sub directory

Hello I used below code to remove extension .php from url in .htaccess index.php -------- index and it worked fine.

Please assist me to stop this from removing the .php extension in sub directories eg: www.example.com/cp/index instead of: www.example.com/cp/index.php

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [NC,L,QSA,R=301]

The following did not work:

RewriteEngine On
RewriteBase /

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

Can I the exception directory one specific call cp

Upvotes: 1

Views: 1026

Answers (1)

Amit Verma
Amit Verma

Reputation: 41219

You can use the following :

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
#####exclude /cp folder####
RewriteCond %{REQUEST_URI} !^/cp
#################
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /((?!cp)[^.]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [NC,L,QSA,R=301]

Upvotes: 2

Related Questions