【问题标题】:Intelligent redirection智能重定向
【发布时间】:2021-01-18 08:05:12
【问题描述】:

我是使用 Apache 2.4.18 的新手。

我想重定向以下格式的 URL。

Current URL: https://www.example.org/page/10/
Desired URL: https://www.example.org/index.php/page/10/

如果我使用以下规则,我可以修改请求,例如第 2 页:

Redirect permanent /page/2/ /index.php/page/2/

但是,我想重定向而不必对我网站上的所有页面进行硬编码。我尝试了以下方法,我的浏览器在多次重定向后失败:

RedirectMatch /page/(.*)/$ /index.php/page/$1/

并且使用以下失败,我不知道为什么:

RedirectMatch "https://www.example.org/page/(.*)/$" "https://www.example.org/index.php/page/$1/"

我做错了什么?

【问题讨论】:

    标签: apache http-redirect


    【解决方案1】:

    在你的队伍中

    RedirectMatch /page/(.*)/$ /index.php/page/$1/

    /page/2/ 实际上会重定向到/index.php/page/2/,但是这个新的 URL仍然会匹配你的 RedirectMatch 的正则表达式并且会产生另一个重定向。这就是为什么它会无休止地重定向,直到浏览器放弃(参见example)。

    我会尝试使用 RedirectMatch ^/page/(.*)/$ /index.php/page/$1/,所以当重定向时,/index.php/page/2/ 将不再匹配 ^/page/(.*)/$,并且不会产生递归重定向。

    作为事后诸葛亮,为什么不改用RewriteRule 呢?它会为客户端保存一个额外的 HTTP 请求,因为当您需要将客户端发送到不同的服务器时,通常会使用重定向。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多