2011-08-23 76 views
0

在Apache重寫時遇到了一些問題。 我的整個網站都運行在SSL(在線商店)上,除了一個頁面(visit_us.php)和谷歌地圖API(因爲谷歌收取$$$$$的HTTPS訪問)。每當這個頁面包含不安全的內容(這對任何最終用戶聽起來都不好)時,這個頁面顯示一條消息,我實現了一個簡單的apache重寫規則切換到端口80,它工作正常。apache rewrite woes

RewriteEngine On 

#redirect all http traffic to https, unless visit_us.php is requested 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} !^/visit_us\.php 
RewriteRule ^(.*)$ https://www.myurl.com/$1 [R=301,L] 

#redirect https traffic for visit_us.php to http 
RewriteCond %{SERVER_PORT} 443 
RewriteCond %{REQUEST_URI} ^/visit_us\.php 
RewriteRule ^(.*)$ http://www.myurl.com/$1 [R=301,L] 

然而,在整合Twitter的微件(只能工作在HTTP),我意識到我將不得不社交網絡頁面添加到工作在端口80 我以爲這是簡單的列表夠了,加上social.php頁面,上面的列表中,像這樣:

RewriteEngine On 

#redirect all http traffic to https, unless visit_us.php or social.php is requested 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} !^/visit_us\.php 
RewriteCond %{REQUEST_URI} !^/social\.php 
RewriteRule ^(.*)$ https://www.myurl.com/$1 [R=301,L] 

#redirect https traffic for visit_us.php and social.php to http 
RewriteCond %{SERVER_PORT} 443 
RewriteCond %{REQUEST_URI} ^/visit_us\.php 
RewriteCond %{REQUEST_URI} ^/social\.php 
RewriteRule ^(.*)$ http://www.myurl.com/$1 [R=301,L] 

在我的網站,我明確地鏈接到HTTP,而不是HTTPS。然而,雖然它仍然適用於visit_us.php頁面,但social.php頁面似乎被忽略,並且請求不斷終止在端口443. 我在做什麼錯誤?

+0

Apache questi ons幾乎總是偏離這個stackoverflow.com。總是有serverfault或網站管理員stckexchange站點。 –

+0

我會銘記未來,謝謝。 – Stann0rz

回答

2
#redirect https traffic for visit_us.php and social.php to http 
RewriteCond %{SERVER_PORT} 443 
RewriteCond %{REQUEST_URI} ^/visit_us\.php 
RewriteCond %{REQUEST_URI} ^/social\.php 
RewriteRule ^(.*)$ http://www.myurl.com/$1 [R=301,L] 

不能使用默認AND邏輯在這裏改寫條件 - 它必須是OR邏輯,而不是(讀簡單的英語你的條件,你會看到破綻)。

兩種方法:

1.明確指定OR邏輯應使用:

#redirect https traffic for visit_us.php and social.php to http 
RewriteCond %{SERVER_PORT} 443 
RewriteCond %{REQUEST_URI} ^/visit_us\.php [OR] 
RewriteCond %{REQUEST_URI} ^/social\.php 
RewriteRule ^(.*)$ http://www.myurl.com/$1 [R=301,L] 

2.合併兩個重寫條件成一個(其中OR邏輯被使用) :

#redirect https traffic for visit_us.php and social.php to http 
RewriteCond %{SERVER_PORT} 443 
RewriteCond %{REQUEST_URI} ^/(visit_us|social)\.php 
RewriteRule ^(.*)$ http://www.myurl.com/$1 [R=301,L] 
+0

啊,沒想到AND是默認的運營商,因爲visit_us還在工作。但後者似乎更合適,效果很好,謝謝你的提示! – Stann0rz