我想要任何请求,例如http://example.com/whatever/index.php
,执行 301 重定向到http://example.com/whatever/
.
我尝试添加:
rewrite ^(.*/)index.php$ $1 permanent;
location / {
index index.php;
}
这里的问题是,此重写在根 URL 上运行,这会导致无限重定向循环。
Edit:
我需要一个通用的解决方案
http://example.com/
应该提供文件webroot/index.php
http://example.com/index.php
,应该 301 重定向到http://example.com/
http://example.com/a/index.php
应该 301 重定向到http://example.com/a/
http://example.com/a/
应该在以下位置提供index.php脚本webroot/a/index.php
基本上,我不想在地址栏中显示“index.php”。我有旧的反向链接,需要重定向到规范网址。
很好的问题,有解决方案类似于我最近在 ServerFault 上回答的另一个问题 https://serverfault.com/questions/568569/nginx-url-rewrite-rule/568902#568902,虽然这里要简单得多,而且您确切地知道自己需要什么。
这里您想要的是仅在用户明确请求时执行重定向/index.php
,但永远不要重定向任何最终由实际服务提供的内部请求index.php
脚本,通过定义index
指示。
这应该做到这一点,避免循环:
server {
index index.php;
if ($request_uri ~* "^(.*/)index\.php$") {
return 301 $1;
}
location / {
# ...
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)