Linux mod_rewrite REQUEST_URI 混淆

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8511431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:39:16  来源:igfitidea点击:

mod_rewrite REQUEST_URI confusion

linuxapachemod-rewritessl

提问by Alpha01

I want to force sslaccess on an entire site, except for the following pages:

我想强制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/
  • 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/

Force ssl:

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 [R=301,L]

However neither of the /cardsor /locations_and_contactsand any of the pages are being excluded from being served with sslaccess. Can someone tell me what's wrong with my set of rules?

然而,/cards/locations_and_contacts和任何页面都不会被排除在ssl访问权限之外。有人能告诉我我的一套规则有什么问题吗?

采纳答案by Olivier Pons

:)

:)

...

...

You forgot the "OR" directive. Here's what should work (and maybe you've forgotten the QSA directive in the redirect as well (maybe not)):

您忘记了“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 [QSA,R=301,L]

Two hints:

两个提示:



Please try to use the RewriteLogdirective: it helps you to track down such problems:

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

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


My favorite tool to check for regexp:

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

http://www.quanetic.com/Regex(don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

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