【问题标题】:Redirect select pages to https将选择页面重定向到 https
【发布时间】:2013-04-10 21:14:11
【问题描述】:
我有一个非常简单的问题,由于某种原因我无法弄清楚,而且数小时的搜索也没有帮助。使用 .htaccess 文件,我如何将 /login.php 和 /index.php 重定向到 https,然后将任何其他页面重定向到 http?我目前使用此代码重定向到 https,但它会重定向每个页面:
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(.*) https://www.ruxim.com/$1 [R]
非常感谢。
【问题讨论】:
标签:
.htaccess
mod-rewrite
ssl
【解决方案1】:
%{SERVER_PORT} 变量取决于配置中的 UseCanonicalPhysicalPort。如果没有设置,那么您可能无法匹配该变量,更容易使用%{HTTPS}。
RewriteCond %{HTTPS} off
RewriteRule ^/?(login|index)\.php https://www.ruxim.com%{REQUEST_URI} [L,R]
RewriteCond %{HTTPS} on
RewriteRule !^/?(login|index)\.php http://www.ruxim.com%{REQUEST_URI} [L,R]
如果您不需要重定向到非 https,则不需要第二条规则。
【解决方案2】:
试试这样的;
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_FILENAME} =index.php [OR]
RewriteCond %{REQUEST_FILENAME} =login.php
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
我注意到 100% 确定 [OR] 是否会在中间突然遵守。
【解决方案3】:
请应用以下条件以仅保护 /login.php 和 /index.php 页面。其他页面将在 HTTP 路径上工作(非安全页面)。
RewriteEngine On
RewriteBase /
# force https for /login.php and /index.php
RewriteCond %{HTTPS} =off
RewriteRule ^(index|login)\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# don't do anything for images/css/js (leave protocol as is)
RewriteRule \.(gif|jpe?g|png|css|js)$ - [NC,L]
# force http for all other URLs
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/(index|login)\.php$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]