2010-03-14 128 views
3

我無法將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

回答

1

您需要將那些導致外部重定向的規則放在這些規則之前,這隻會導致內部重定向。所以:

# 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

0

這並不是檢測服務器端口是什麼正確的做法,應該遵循相同的規則分別使用^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