.htaccess用ssl重定向url重写

问题描述:

我无法将url查询参数重写(花式url)与.htaccess ssl重定向结合起来。.htaccess用ssl重定向url重写

我的.htaccess文件是目前:

Options +FollowSymLinks 
Options -Indexes 
ServerSignature Off 
RewriteEngine on 
RewriteBase/

# in https: process secure.html in https 
RewriteCond %{server_port} =443 
RewriteCond $1 ^secure$ [NC] 
RewriteRule ^(.+).html$ index.php?page=$1 [QSA,L] 

# in https: force all other pages to http 
RewriteCond %{server_port} =443 
RewriteCond $1 !^secure$ [NC] 
RewriteRule ^(.+).html$ http://%{HTTP_HOST}%{REQUEST_URI} [QSA,N] 

# in http: force secure.html to https 
RewriteCond %{server_port} !=443 
RewriteCond $1 ^secure$ [NC] 
RewriteRule ^(.+).html$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,N] 

# in http: process other pages as http 
RewriteCond %{server_port} !=443 
RewriteCond $1 !^secure$ [NC] 
RewriteRule ^(.+).html$ index.php?page=$1 [QSA,L] 

的花式URL重写工作正常,但重定向/从https是不工作的。

如果我

RewriteRule ^(.+).html$ https://%{HTTP_HOST}/index.php?page=$1 [QSA,L] 

替换包含

RewriteRule ^(.+).html$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,N] 

2线则HTTPS重定向工作正常,但花哨,URL重写无法正常工作。

这两者可以结合吗?

编辑:

所期望的结果是:

1. http://domain.com/secure.html is rewritten to https://domain.com/index.php?page=secure 
2. http://domain.com/foo.html is rewritten to http://domain.com/index.php?page=foo 
3. https://domain.com/secure.html is rewritten to https://domain.com/index.php?page=secure 
4. https://domain.com/foo.html is rewritten to http://domain.com/index.php?page=foo 

(我不得不把它们放在一个代码块为超过1个链接是不允许新用户)

所以secure.html始终是https,而foo.html(所有其他页面)始终是http。

解决方案:

由于浓汤,解决的办法是:

Options +FollowSymLinks 
Options -Indexes 
ServerSignature Off 
RewriteEngine on 
RewriteBase/

# in https: force all other pages to http 
RewriteCond %{server_port} =443 
RewriteCond $1 !^secure$ [NC] 
RewriteRule ^(.+)\.html$ http://%{HTTP_HOST}%{REQUEST_URI} [QSA,N] 

# in http: force secure.html to https 
RewriteCond %{server_port} !=443 
RewriteCond $1 ^secure$ [NC] 
RewriteRule ^(.+)\.html$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,N] 

# in https: process secure.html in https 
RewriteCond %{server_port} =443 
RewriteCond $1 ^secure$ [NC] 
RewriteRule ^(.+)\.html$ index.php?page=$1 [QSA,L] 

# in http: process other pages as http 
RewriteCond %{server_port} !=443 
RewriteCond $1 !^secure$ [NC] 
RewriteRule ^(.+)\.html$ index.php?page=$1 [QSA,L] 
+0

你能举一个你预计会发生什么和发生什么的例子吗? – Gumbo 2010-03-14 18:39:19

您需要将那些导致外部重定向的规则放在这些规则之前,这只会导致内部重定向。所以:

# in https: force all other pages to http 
RewriteCond %{SERVER_PORT} =443 
RewriteCond $1 !^secure$ [NC] 
RewriteRule ^(.+)\.html$ http://%{HTTP_HOST}%{REQUEST_URI} [QSA,L] 

# in http: force secure.html to https 
RewriteCond %{SERVER_PORT} !=443 
RewriteCond $1 ^secure$ [NC] 
RewriteRule ^(.+)\.html$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,L] 

# in https: process secure.html in https 
RewriteCond %{SERVER_PORT} =443 
RewriteCond $1 ^secure$ [NC] 
RewriteRule ^(.+)\.html$ index.php?page=$1 [QSA,L] 

# in http: process other pages as http 
RewriteCond %{SERVER_PORT} !=443 
RewriteCond $1 !^secure$ [NC] 
RewriteRule ^(.+)\.html$ index.php?page=$1 [QSA,L] 
+0

Gumbo这个作品是一种享受 - 感谢一个负载! – 2010-03-14 19:37:36

这并不是检测服务器端口是什么正确的做法,应该遵循相同的规则分别使用^443$!^443的http_host。你真的应该利用server_port,这是很好的做法。这是一个good little tutorial可能会帮助你。

+0

当我开始时,^ 443 $方法在我的服务器上不起作用,而上面的= 443方法是 - 我再给它一个确认。 – 2010-03-14 18:53:38

+0

'= 443'和'^ 443 $'在语义上是相同的。前者只是一种词典(平等)比较,后者则是正则表达式比较。 – Gumbo 2010-03-14 19:24:24