2015-02-11 146 views
-1

我們正在運行包含HTTP和HTTP協議的網站。我們需要一個針對.htaccess文件的規則,我們可以處理這些請求,即非https頁面應該使用http://重定向,並且https頁面應該使用https://協議重定向。 我們使用的是Apache。任何幫助將不勝感激。在單個網站上的http和https頁面之間切換

+0

請提供一些關於重定向規則應該是什麼樣的細節。 Ps:我不是downvoter。 – 2015-02-11 13:37:03

+0

謝謝Ravi,我在我的網站上使用這兩種協議。例如,讓我們分兩頁,不使用ssl的主頁和使用ssl的聯繫頁。所以如果有人試圖使用https://homepage.com訪問主頁,那麼他必須重定向到http://homepage.com。與使用ssl的聯繫人頁面相反的情況。 – 2015-02-11 13:39:29

回答

2

將此添加到您站點根目錄的.htaccess文件中。

RewriteEngine on 

# Force http:// 
RewriteCond %{HTTPS} =on 
RewriteCond %{REQUEST_URI} ^(/|/dl/memorandum.*)$ [NC] 
RewriteRule^http://%{HTTP_HOST}/ [R=301,L] 

# Force SSL 
RewriteCond %{HTTPS} !=on 
RewriteCond %{REQUEST_URI} ^/contact$ [NC] 
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

您可以通過使用|或操作者匹配在多個頁面。

^/(contact|profile|transactions)$ [NC] 

如果網址冗長,你可以刪除$,做一個前綴匹配的

^/(contact|profile|transactions) [NC] 

這將匹配/contact-page/contact/page以及現在。


使用否定在所有不需要SSL的規則上強制使用HTTP。

RewriteEngine on 

# Force http:// 
RewriteCond %{HTTPS} =on 
RewriteCond %{REQUEST_URI} !^/(contact|login|cart|register|forgot-your-password|request)$ [NC] 
RewriteRule^http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

# Force SSL 
RewriteCond %{HTTPS} !=on 
RewriteCond %{REQUEST_URI} ^/(contact|login|cart|register|forgot-your-password|request)$ [NC] 
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 
+0

如何在此行添加多個網址「 RewriteCond%{REQUEST_URI} ^/contact $」 – 2015-02-11 13:49:23

+0

答覆已更新。 – 2015-02-11 13:55:33

+0

似乎有效,但有錯誤。 看到這個鏈接:https://www.netlawman.co.in/dl/memorandum-and-articles-of-association這是非https頁面,但仍然打開與https://應該重定向到http: // – 2015-02-11 14:02:00