如何使用.htaccess将所有页面重定向到https,但有例外?

时间:2012-05-07 00:08:33

标签: php apache .htaccess redirect ssl

我已经使用htaccess强制所有页面使用...

重新路由到https
RewriteEngine on
RewriteBase /

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://ourdomain.com/$1 [R,L]

我有什么方法可以为mydomain.com/videos&& ourdomain.com/blog确保这些页面被强制删除https并使用http?

3 个答案:

答案 0 :(得分:6)

您已经使用RewriteCond来限制重写到端口80上的请求。

想要在{80}的/videos/blog上重写请求,所以:

RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/videos
RewriteCond %{REQUEST_URI} !^/blog
RewriteRule ^(.*)$ https://ourdomain.com/$1 [R,L]

你想在端口上重写这些URL的请求(大概)443:

RewriteCond %{SERVER_PORT} 443
RewriteRule ^/((videos|blog)/.*) http://ourdomain.com/$1 [R,L]

答案 1 :(得分:2)

尝试

RewriteEngine on
RewriteBase /

RewriteCond %{SERVER_PORT} 443
RewriteRule ^blog/?(.*) http://ourdomain.com/blog/$1 [R,L]

RewriteCond %{SERVER_PORT} 443
RewriteRule ^videos/?(.*) http://ourdomain.com/videos/$1 [R,L]

RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/videos
RewriteCond %{REQUEST_URI} !^/blog
RewriteRule ^(.*)$ https://ourdomain.com/$1 [R,L]

答案 2 :(得分:0)

另一种解决方案。我希望它会有所帮助。

RewriteEngine on
Rewritebase /


RewriteCond %{SERVER_PORT} !^443$ [OR]
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{ENV:HTTPS} =off
RewriteRule ^(/(.*))?$ https://%{HTTP_HOST}/$1 [R=301,L]