【问题标题】:How do I force SSL for some URLs and force non-SSL for all others?如何对某些 URL 强制使用 SSL,而对所有其他 URL 强制使用非 SSL?
【发布时间】:2011-02-15 17:00:35
【问题描述】:

我想确保我网站上的某些 URL 始终通过 HTTPS 访问,而所有其他 URL 都通过 HTTP 访问。

我可以在我的 .htaccess 文件中使用任何一种情况,但是如果我同时启用这两种情况,那么我会获得无限重定向。

我的 .htaccess 文件是:

<IfModule mod_expires.c>
# turn off the module for this directory
ExpiresActive off
</IfModule>

Options +FollowSymLinks
AddHandler application/x-httpd-php .csv

RewriteEngine On

RewriteRule ^/?registration(.*)$ /register$1 [R=301,L]

# Force SSL for certain URL's
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (login|register|account)
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Force non-SSL for certain URL's
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !(login|register|account)
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Force files ending in X to use same protocol as initial request
RewriteRule \.(gif|jpg|jpeg|jpe|png|ico|css|js)$ - [S=1]

# Use index.php as the controller
RewriteCond %{REQUEST_URI} !\.(exe|css|js|jpe?g|gif|png|pdf|doc|txt|rtf|xls|swf|htc|ico)$ [NC]
RewriteCond %{REQUEST_URI} !^(/js.*)$
RewriteRule ^(.*)$ index.php [NC,L]

有人对我如何强制登录、注册和帐户页面为 https,同时强制其他所有页面不为 https 有任何建议吗?

【问题讨论】:

  • 什么 url 触发了无限重定向?重写日志的输出是什么?
  • 你应该知道,通过服务器知道他们在“其他”页面上,SSL的所有辛苦工作已经完成,强制它为非SSL只是额外的工作。跨度>

标签: apache mod-rewrite ssl https


【解决方案1】:

您似乎为 SSL 匹配 HTTPS=on,为未加密连接匹配 HTTPS=off。

您应该仔细检查 HTTPS 环境变量是否按预期设置。否则会有一个无限的重定向 - 正如你所经历的那样。

例如:https://issues.apache.org/bugzilla/show_bug.cgi?id=50581

【讨论】:

    猜你喜欢
    • 2011-03-19
    • 1970-01-01
    • 2010-12-05
    • 2014-01-22
    • 1970-01-01
    • 2015-07-14
    • 2017-01-02
    • 2020-07-03
    • 1970-01-01
    相关资源
    最近更新 更多