Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent loading pages with .php file extension (only load without it)

Tags:

php

.htaccess

I have a rewrite condition in my .htaccess file which removes the need for .php file extension

RewriteEngine On
RewriteRule ^([^.]+)$ $1.php

so http://site.com/blog opens http://site.com/blog.php

but if old users type /blog.php it will also load the page

is there a way to prevent or redirect pages with .php or any other file extention to the one without it?

i mean if user entered /blog.php or /blog.asp it should either fail to load or redirect to /blog (without extention)

like image 622
Vladimir Avatar asked Oct 26 '13 12:10

Vladimir


1 Answers

A better way to accomplish this would be to only rewrite if a .php by that name exists. Otherwise throw 404 for the original URL. The second set of rules would take care of removing the extension and avoiding the redirect loop.

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

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

RewriteCond %{THE_REQUEST} ^(?:GET|POST)\ /.*\.php\ HTTP.*$ [NC]
RewriteRule ^(.*)\.php$ $1 [R=301,L]
like image 178
Ravi K Thapliyal Avatar answered Sep 24 '22 02:09

Ravi K Thapliyal