简体   繁体   English

防止以.php文件扩展名加载页面(仅在没有它的情况下加载)

[英]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 我的.htaccess文件中有一个重写条件,它不需要.php文件扩展名

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

so http://site.com/blog opens http://site.com/blog.php 所以http://site.com/blog打开http://site.com/blog.php

but if old users type /blog.php it will also load the page 但如果旧用户键入/blog.php,它也会加载页面

is there a way to prevent or redirect pages with .php or any other file extention to the one without it? 有没有办法防止或重定向页面与.php或任何其他文件扩展名没有它?

i mean if user entered /blog.php or /blog.asp it should either fail to load or redirect to /blog (without extention) 我的意思是如果用户输入/blog.php或/blog.asp它应该无法加载或重定向到/ blog(没有扩展)

A better way to accomplish this would be to only rewrite if a .php by that name exists. 实现此目的的更好方法是仅在存在该名称的.php时才重写。 Otherwise throw 404 for the original URL. 否则为原始URL抛出404 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. 这会导致apache将重定向发送回浏览器,这会将其URL更新为从扩展名中剥离的URL。 But make sure to place this rule in front of the one the redirects to .php internally 但请确保将此规则放在内部重定向到.php的规则之前

尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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