.htaccess仅在某些页面上设置SSL重定向时无法正常工作

时间:2013-10-29 14:15:19

标签: .htaccess mod-rewrite ssl

在寻找上述问题的答案时,我遇到了这个脚本作为解决方案(我希望我的网站在http上除了登录,注册,管理等几个页面之外):

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

# force https for /login.php and /register.php
RewriteCond %{HTTPS} =off
RewriteRule ^(login|register)\.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} !^/(login|register)\.php$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

这个确切的示例应该可以在我的服务器上运行,但它只能部分工作,这意味着它将所有页面重定向到http,但我无法打开login.php或register.php。 webbrowser声明这些页面包含重定向循环,但它们无法显示。我绝不是mode_rewrite或htaccess的专家,所以我将不胜感激。

编辑: 我已经按照建议操作并使用chrome插件运行mywebsite。我发现在页面中login.php有重定向循环http-> https-> http等(该页面上唯一的其他重定向是当用户已经签名时,但它似乎不是为了这个环)。我也尝试了不同的代码来设置SSL,这个有效:

# Rewrite Rules for example.com
RewriteEngine On
RewriteBase /

# Turn SSL on for payments
RewriteCond %{HTTPS} off
RewriteCond %{SCRIPT_FILENAME} \/login\.php [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Turn SSL off everything but payments
RewriteCond %{HTTPS} on
RewriteCond %{SCRIPT_FILENAME} !\/login\.php [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

这个工作正常,但它只有一个例外,我想要更多的(约6个)。有人知道为什么第一个脚本无效吗?或者如何修改第二个以获得更多https网站?

由于

1 个答案:

答案 0 :(得分:1)

我有类似的问题,这对我有用。

在httpd.conf中,在虚拟主机下,确保同时具有:

ServerName domain.com

ServerAlias www.domain.com

VirtualHost中的两个*:80和VirtualHost *:443

相关问题