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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21687288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:22:39  来源:igfitidea点击:

nginx redirect loop, remove index.php from url

nginxphp

提问by jcampbell1

I want any requests like http://example.com/whatever/index.php, to do a 301 redirect to http://example.com/whatever/.

我想要任何类似的请求http://example.com/whatever/index.php,将 301 重定向到http://example.com/whatever/.

I tried adding:

我尝试添加:

rewrite ^(.*/)index.php$  permanent;

location / {
    index  index.php;
}

The problem here, this rewrite gets run on the root url, which causes a infinite redirect loop.

这里的问题是,这个重写在根 url 上运行,这会导致无限重定向循环。

Edit:

编辑:

I need a general solution

我需要一个通用的解决方案

http://example.com/should serve the file webroot/index.php

http://example.com/应该提供文件 webroot/index.php

http://example.com/index.php, should 301 redirect to http://example.com/

http://example.com/index.php, 应该 301 重定向到 http://example.com/

http://example.com/a/index.phpshould 301 redirect to http://example.com/a/

http://example.com/a/index.php应该 301 重定向到 http://example.com/a/

http://example.com/a/should serve the index.php script at webroot/a/index.php

http://example.com/a/应该在以下位置提供 index.php 脚本 webroot/a/index.php

Basically, I never want to show "index.php" in the address bar. I have old backlinks that I need to redirect to the canonical url.

基本上,我从不想在地址栏中显示“index.php”。我有旧的反向链接,需要重定向到规范网址。

回答by cnst

Great question, with the solution similar to another one I've answered on ServerFault recently, although it's much simpler here, and you know exactly what you need.

很好的问题,其解决方案类似于我最近在 ServerFault 上回答的另一个解决方案,尽管这里简单得多,而且您确切地知道自己需要什么。

What you want here is to only perform the redirect when the user explicitly requests /index.php, but never redirect any of the internal requests that end up being served by the actual index.phpscript, as defined through the indexdirective.

您在这里想要的是仅在用户明确请求时执行重定向/index.php,但永远不要重定向最终由实际index.php脚本提供服务的任何内部请求,如通过index指令定义的。

This should do just that, avoiding the loops:

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

server {
    index index.php;

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

    location / {

        # ...
    }
}

回答by dev691

Try that

试试看

location ~ /*/index.php {
    rewrite ^/(.*)/(.*) http://www.votre_domaine.com/ permanent;
}
location /index.php {
    return 301 http://www.example.com/;
}

回答by Melvyn

Keep the first slash out of the match :

将第一个斜线排除在比赛之外:

rewrite ^/(. +)/index.php$ $scheme:/// permanent;

回答by abradley

Try

尝试

location = /whatever/index.php {
    return 301 $scheme://www.example.com/whatever/;
}

Another benefit from doing it this way is that nginx does a return faster than a rewrite.

这样做的另一个好处是 nginx 比重写更快地返回。