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

编程入门 行业动态 更新时间:2024-10-13 06:15:32
本文介绍了nginx 重定向循环,从 url 中删除 index.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I want any requests like example/whatever/index.php, to do a 301 redirect to example/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

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

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

example/index.php, should 301 redirect to example/

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

example/a/index.php should 301 redirect to example/a/

example/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

本文发布于:2023-11-02 03:49:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551287.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   nginx   url   php   index

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!