【问题标题】:How to redirect all URLs except one path and all its subpaths without mod_rewrite?如何在没有 mod_rewrite 的情况下重定向除一个路径及其所有子路径之外的所有 URL?
【发布时间】:2015-04-19 18:56:01
【问题描述】:

我在 Tomcat 7 之前安装了 Apache 2.4(尽管我认为前者很重要)。 我需要将域上的所有请求重定向到 HTTPS 端口,而不是那些转到特定文件夹的请求:

(1) http://www.example.com/ -> https://www.example.com/
(2) http://www.example.com/products -> https://www.example.com/products
(3) http://www.example.com/... -> https://www.example.com/...

同时

(4) http://www.example.com/services should pass straight 
(5) http://www.example.com/services/main should pass straight
(6) http://www.example.com/services/main/list?params should pass straight

如果不使用mod_rewrite,我怎样才能做到这一点(正如我在其他问题中所说的那样)?

我在文档中看到使用 RedirectRedirectMatch 指令处理重定向。

所以我可以处理 1,2,3:

<VirtualHost *:80>
    ...
    ServerName www.example.com
    Redirect / https://www.example.com
</VirtualHost>

但我无法编写正则表达式来处理其他三个。我在this answer 中发现我可以使用否定的环视,但是像/(?!services).* 这样的正则表达式只会匹配4(至少根据Regex Tester on

编辑

我已指定无需mod_rewrite 即可完成此操作。

【问题讨论】:

    标签: regex redirect apache2.4


    【解决方案1】:

    在此处使用基于正则表达式的重定向规则:

    <VirtualHost *:80>
        ...
        ServerName www.example.com
        RedirectMatch 301 ^/((?!services).*)$ https://www.example.com/$1
    </VirtualHost>
    

    (?!services) 是一个否定的前瞻,它将除以 /services 开头的任何 URL 之外的任何 URL

    【讨论】:

    • 这没有捕捉到被重定向的/services/other
    • 不,它将跳过以/services 开头的任何内容。在不同的浏览器中测试。
    • 对不起,我的错。那行得通。是的,我确实重启了 Apache,只是没有修复其他指令。
    • 如何使用 .htaccess 实现这一目标
    猜你喜欢
    • 2010-09-06
    • 2020-03-31
    • 2011-05-10
    • 2016-02-25
    • 1970-01-01
    • 2021-01-12
    • 2021-03-24
    • 2014-01-21
    • 1970-01-01
    相关资源
    最近更新 更多