【问题标题】:Set apache redirect from http to https for a specific url为特定的 url 设置从 http 到 https 的 apache 重定向
【发布时间】:2012-04-05 14:43:12
【问题描述】:

我想将任何具有 /test 的 URL 路径重定向到 https://localhost/test。此外,如果 url 是 /test?user=123 它必须重定向到 https://localhost/test?user=123 或者如果 url 是 /test/test_db/user?id=123&pwd=123 必须重定向到 https://localhost/test/user/test_db/user?id=123&pwd=123

任何类型的所有其他请求都必须重定向到根文件夹 (http://localhost/accessdenied.html) 中显示“拒绝访问”的 html 页面。

如何在 apache 中使用 RedirectMatch 来实现这一点。我尝试了类似的东西

RedirectMatch permanent ^test/(.*)$ https://localhost/test/$1

这不起作用。

【问题讨论】:

    标签: redirect apache httpd.conf mod-alias


    【解决方案1】:

    我想知道这是否更好作为 ServerFault 问题。

    无论如何:
    我不知道如何使用 RedirectMatch 实现这一点,但我知道如何使用 ModRewrite 做到这一点:

    RewriteEngine On$
    RewriteCond %{HTTPS} off$
    RewriteRule ^/bla https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]$
    

    这是一种相当通用的形式,适用于任何 HTTP 主机(因为我不知道有关您的主机的任何详细信息),并将重定向与 URL 开头的 bla 匹配的任何内容,而不是已经是 Https,带有参数和一切。

    【讨论】:

    • 这对我有用,它回答了这个问题。我认为这应该被@Abhishek 标记为接受,即使它已经超过一年了。
    • 不要在行尾为我添加 $ RewriteRule ^/yourpath https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    【解决方案2】:

    这在我的 Apache httpd 2.2 服务器上非常适合我:

    RedirectMatch 301 ^(/someprefix[^/]*/.*)$ https://hostname$1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-17
      • 2018-04-09
      • 2019-12-17
      • 2013-09-27
      • 2016-02-04
      • 2011-06-11
      • 1970-01-01
      • 2018-04-17
      相关资源
      最近更新 更多