我希望像http://example.com/whatever/index.php这样的任何请求都能执行301号重定向到http://example.com/whatever/。
我试着补充:
rewrite ^(.*/)index.php$ $1 permanent;
location / {
index index.php;
}这里的问题是,这个重写在根url上运行,这会导致无限重定向循环。
编辑:
我需要一个通用的解决方案
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/应该在webroot/a/index.php上提供index.php脚本
基本上,我从来不想在地址栏中显示"index.php“。我有旧的反向链接,我需要重定向到规范url。
发布于 2014-02-16 16:11:37
使用解决方案similar to another one I've answered on ServerFault recently,这是一个很好的问题,尽管在这里它要简单得多,而且您知道您需要什么。
这里您想要的是只在用户显式请求/index.php时执行重定向,而不是像通过index指令定义的那样,重定向任何最终由实际的index.php脚本提供服务的内部请求。
这应该做到这一点,避免循环:
server {
index index.php;
if ($request_uri ~* "^(.*/)index\.php$") {
return 301 $1;
}
location / {
# ...
}
}发布于 2014-02-14 23:51:36
试试看
location ~ /*/index.php {
rewrite ^/(.*)/(.*) http://www.votre_domaine.com/$1 permanent;
}
location /index.php {
return 301 http://www.example.com/;
}发布于 2020-08-07 13:25:42
如果您已经在Nginx配置文件中提到了下面的第一行,那么您将不再重写它。
索引index.php index.html index.htm;
重写^(/.).html(?)?$1$2永久;
重写^/(.*)/$ /$1永久;
try_files $uri/index.html $uri.html $uri/ $uri =404;
这将从网址中删除.html,并且还将从主页或索引页删除“索引”。例如- https://www.example.com/index将改为https://www.example.com
https://stackoverflow.com/questions/21687288
复制相似问题