Luis Edward Miranda
Luis Edward Miranda

Reputation: 43

How to hide file extension using .htaccess + Restricting access

Guys how to remove a file extension using .htaccess. example : file name was "/home.php"

of course after using .htaccess

it can now access using link "/home"

it is possibly to redirect using into 404 error if they visit "/home.php" instead of "/home"

Thanks!

Upvotes: 3

Views: 62

Answers (3)

Jon Lin
Jon Lin

Reputation: 143886

You can just use Multiviews:

Options +Multiviews
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+([^\?\ ]+)\.php
RewriteRule ^ /%1 [L,R=301]

This redirects /home.php to /home, but you can replace the 301 in the square brackets with 404 if you insist on returning a "not found".

Upvotes: 3

tomloprod
tomloprod

Reputation: 7862

You can use this rewrite rule in your .htaccess to remove the php extension

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

Regards,

Source: http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/

Upvotes: 0

Uri Goren
Uri Goren

Reputation: 13692

This snippet will rewrite *.php to *

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

Upvotes: 0

Related Questions