简体   繁体   中英

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

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)

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]
RewriteEngine On
RewriteRule ^([^.]+)$ $1.php [L]
RewriteRule ^(.*?)\.php$ $1 [R=301,L]

You can use the rule

RewriteRule ^(.+)\.php$ $1 [R=301,L]

This would cause apache to send back a redirect to the browser which would update it's URL to the one stripped from the extension. But make sure to place this rule in front of the one the redirects to .php internally

尝试这个:

RewriteRule ^(.*?)\.([a-z0-9]+)$ $1 [R=301,L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM