0

当我启用 MultiViews 时,如果我访问错误的 URL,我的页面 (index.php) 仍然会到达,而我希望用户收到 404 错误。我试图弄清楚如何在不创建规则的情况下解决此问题.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”)。

4

2 回答 2

0

您可以只使用以下内容,因此.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-04T02:07:11.070 回答
0

我找到了一个适合我的解决方案。

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]

来源:链接

于 2017-01-04T05:34:47.580 回答