当我启用MultiViews时,如果我访问坏的URL,当我希望用户得到一个404错误时,我的页面(index.php)仍然会被访问。我试图找出如何在不创建.htaccess规则的情况下解决这个问题。
例如,"www.mydomain.com/index/blah/blah",访问index.php,但是我希望它失败,因为附带的拖尾垃圾URL组件。类似地,对于"/contact/blah/awuihda/hiu",(显示contact.php的内容),应该提供一个404错误,因为"/blah/awuihda/hiu“不存在。
如果我禁用MultiViews,它可以正常工作,但是我不能像我想要的那样缩写这个URL (例如,不能输入"/contact“来显示"contact.php")。
发布于 2017-01-04 02:07:11
您可以使用以下方法,这样就不需要.php扩展了,这是通常的方法:
RewriteEngine on
# Remove .php if it's present with a 301 redirect
RewriteRule ^(.+)\.php$ $1 [R=301,L]
# If a request doesn't exist as a directory, file or symlink
# and it does exist with .php appended, rewrite to that
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L]我知道它在.htaccess中添加了一条规则,但这是一种不适用于所有东西的规则,也意味着您不会触及潜在的重复内容,允许使用或不使用.php (实际上,在它之后的任何内容都会跟随在您的示例中)提供相同的内容。希望它有用。
它可以进入主服务器配置,但需要修改。
发布于 2017-01-04 05:34:47
我找到了一个对我有用的解决方案。
Options -Indexes SymLinksIfOwnerMatch -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]来源:link
https://stackoverflow.com/questions/41455019
复制相似问题