user1012181
user1012181

Reputation: 8726

.htaccess file not working for extension hiding

I'm using the following code in the .htaccess file to hide the extension of the php file.

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^ ([^\.]+)$ $1.php [NC]

I placed this file in the localhost/obis/ But when I run the index.php file without the .php extension, the following error occurs.

The requested URL /obis/index was not found on this server.

Can anybody suggest what might be the error?

I took the code from this tutorial: http://www.youtube.com/watch?v=L6k_WvvpPpk

Upvotes: 1

Views: 203

Answers (1)

anubhava
anubhava

Reputation: 785266

Try these 2 rules in root .htaccess:

RewriteEngine On

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

RewriteCond %{THE_REQUEST} /index [NC]
RewriteRule ^(.*?)index([/.]|$) /$1 [L,R=302,NC,NE]

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

Upvotes: 2

Related Questions