nginx 重定向循环,从 url 中删除 index.php [英] nginx redirect loop, remove index.php from url

查看:33
本文介绍了nginx 重定向循环,从 url 中删除 index.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要任何像 http://example.com/whatever/index.php 的请求,做一个 301 重定向到 http://example.com/whatever/.

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

我尝试添加:

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

location / {
    index  index.php;
}

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

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

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

I need a general solution

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

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

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

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

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

http://example.com/a/ 应该在 webroot/a/index.php

基本上,我不想在地址栏中显示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.

推荐答案

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

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.

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

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.php script, as defined through the index directive.

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

This should do just that, avoiding the loops:

server {
    index index.php;

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

    location / {

        # ...
    }
}

这篇关于nginx 重定向循环,从 url 中删除 index.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆