首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >nginx重定向循环,从url中删除index.php

问nginx重定向循环,从url中删除index.php
EN

Stack Overflow用户
提问于 2014-02-10 20:31:02
回答 4查看 29K关注 0票数 28

我希望像http://example.com/whatever/index.php这样的任何请求都能执行301号重定向到http://example.com/whatever/。

我试着补充:

代码语言:javascript
复制
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。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-02-16 16:11:37

使用解决方案similar to another one I've answered on ServerFault recently,这是一个很好的问题,尽管在这里它要简单得多,而且您知道您需要什么。

这里您想要的是只在用户显式请求/index.php时执行重定向,而不是像通过index指令定义的那样,重定向任何最终由实际的index.php脚本提供服务的内部请求。

这应该做到这一点,避免循环:

代码语言:javascript
复制
server {
    index index.php;

    if ($request_uri ~* "^(.*/)index\.php$") {
        return 301 $1;
    }

    location / {

        # ...
    }
}
票数 72
EN

Stack Overflow用户

发布于 2014-02-14 23:51:36

试试看

代码语言:javascript
复制
location ~ /*/index.php {
    rewrite ^/(.*)/(.*) http://www.votre_domaine.com/$1 permanent;
}
location /index.php {
    return 301 http://www.example.com/;
}
票数 2
EN

Stack Overflow用户

发布于 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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21687288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档