【问题标题】:mod_rewrite REQUEST_URI confusionmod_rewrite REQUEST_URI 混淆
【发布时间】:2012-01-20 15:18:26
【问题描述】:

我想强制ssl 访问整个网站,但以下页面除外:

  • www.example.com/loans_and_lines/home_loans.html www.example.com/cards
  • www.example.com/cards/international-ministries.html
  • www.example.com/cards/international-ministries/donation-3.html
  • www.example.com/locations_and_contacts/

强制ssl:

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/personal/loans_and_lines/home_loans\.html
RewriteCond %{REQUEST_URI} !^/cards/$
RewriteCond %{REQUEST_URI} !^/cards/international\-ministries\.html
RewriteCond %{REQUEST_URI} !^/cards/international\-ministries/donation\-3\.html
RewriteCond %{REQUEST_URI} !^/locations_and_contacts/$
RewriteRule (.*) https://www.example.com$1 [R=301,L]

但是,/cards/locations_and_contacts 以及任何页面都不会被排除在ssl 访问权限之外。谁能告诉我我的规则有什么问题?

【问题讨论】:

    标签: linux apache mod-rewrite ssl


    【解决方案1】:

    :)

    ...

    您忘记了“OR”指令。这是应该起作用的(也许你也忘记了重定向中的 QSA 指令(也许不是)):

    # Force SSL
    RewriteCond %{HTTPS} off
    RewriteCond %{REQUEST_URI} !^/personal/loans_and_lines/home_loans\.html [OR]
    RewriteCond %{REQUEST_URI} !^/cards/$ [OR]
    RewriteCond %{REQUEST_URI} !^/cards/international\-ministries\.html [OR]
    RewriteCond %{REQUEST_URI} !^/cards/international\-ministries/donation\-3\.html [OR]
    RewriteCond %{REQUEST_URI} !^/locations_and_contacts/$
    RewriteRule (.*) https://www.example.com$1 [QSA,R=301,L]
    

    两个提示:


    请尝试使用RewriteLog 指令:它可以帮助您追踪此类问题:

    # Trace:
    # (!) file gets big quickly, remove in prod environments:
    RewriteLog "/web/logs/mywebsite.rewrite.log"
    RewriteLogLevel 9
    RewriteEngine On
    

    我最喜欢的检查正则表达式的工具:

    http://www.quanetic.com/Regex(别忘了选择 ereg(POSIX) 而不是 preg(PCRE)!)

    【讨论】:

    • 哇,我完全忘记了 [OR] 指令。
    • 您可以在 2 行中优化您的重写条件,但它的“可读性”会降低。
    • [OR] 在此特定示例中不起作用,因为它仍会将所有请求重定向到 HTTPS。
    • 您的答案中有 cmets 作为答案发布:i.stack.imgur.com/i8piK.png
    • 您的回答在我看来在逻辑上是错误的。您不能使用[OR],因为这些选项是互斥的。这将触发每个请求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    • 1970-01-01
    相关资源
    最近更新 更多