Lucas Ferraz
Lucas Ferraz

Reputation: 740

How to hide my .html and .php extensions using .htaccess?

I have this .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z,0-9,A-Z,_-]+)$ ./$1.php


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z,0-9,A-Z,_-]+)$ ./$1.html

The problem is: Its just hidding .php Shouldn't it hide .php and .html?

Upvotes: 1

Views: 1633

Answers (2)

pixeline
pixeline

Reputation: 17974

1./ your html files are still accessible because your second set of rule will never be used. Besides, your second line specifically tells the server to leave "real" files aside.

2./ You only need RewriteEngine On once .

3./ You should use the flags to tell your rewriting when to stop.

RewriteEngine On
# if the requested file does not exist
RewriteCond %{REQUEST_FILENAME} !-f
# if the requested folder does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# sends all urls except home to its corresponding php file
RewriteRule ^([a-z,0-9,A-Z,_-]+)$ ./$1.php [R=301,L]

But if you want it to work for all file extensions, remove the first rule, leaving only.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z,0-9,A-Z,_-]+)$ ./$1.php [R=301,L]

Upvotes: 0

anubhava
anubhava

Reputation: 785128

Change your rules to:

RewriteEngine On

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

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

Upvotes: 1

Related Questions