Alexandre Cristo
Alexandre Cristo

Reputation: 331

Hide file extension out of folders with .htaccess

My website is like organized like this:

I want to remove the extensions only from my files at root. So in this case, index.php should be index and products.php should be products but I don't want to touch on the file extensions inside folders. So file_manager.php and another_file.php should stay with the extensions

At this point my code is like this:

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

but this code removes extensions from folders do.

How can I change this?

Upvotes: 1

Views: 25

Answers (1)

anubhava
anubhava

Reputation: 785128

Tweak your regex to ensure that it affects root files only:

RewriteEngine on

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