2010-03-01 103 views
2

我知道有很多關於我的問題的主題。我檢查了他們,並試用了它們,但無法使其工作。 我只需要在某些頁面上將http重寫爲https。訪問https頁面後,URL將返回到http。 這是我到目前爲止有:僅在使用.htaccess的某些頁面上將http重寫爲https


# Rewrite Rules for domain.com 
RewriteEngine On 
RewriteBase/

#Rewrite www to domain.com 
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC] 
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301] 

#Rewrite to https 
RewriteCond %{HTTPS} off 
RewriteCond %{REQUEST_URI} /secure.php 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L] 

#traffic to http://, except secure.php 
RewriteCond %{HTTPS} off 
RewriteCond %{REQUEST_URI} /secure.php 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L] 

當我跳過了最後一組規則(流量爲http://,除了secure.php)的secure.php頁面加載的HTTPS和加密。精細。使用最後一組規則(到http://的流量,除secure.php之外),URL重寫爲https,變成藍色(SSL)一秒鐘後消失(無SSL),但URL仍然是https。

有什麼想法? Jacek

+0

你的第三套RewriteCond和RewriteRule正如你在這裏所說的那樣,與第二套相同。 – 2011-11-19 03:20:14

回答

4

第三組規則存在錯誤。 下面是解決方案:使用第三組規則:如果https是「開」,那麼如果URL不包含「/ secure」,則重定向到http。

# Rewrite Rules for domain.com 
RewriteEngine On 
RewriteBase/

#Rewrite www to domain.com 
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC] 
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301] 

#Rewrite to https 
RewriteCond %{HTTPS} off 
RewriteCond %{REQUEST_URI} /secure.php 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L] 

#traffic to http://, except secure.php 
RewriteCond %{HTTPS} on 
RewriteCond %{REQUEST_URI} !(/secure.php) 
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L] 

題外話建議:請認準雅虎慢於網站優化的他們非常聰明的建議,你就會明白爲什麼它總是最好有之前的「WWW」。你最好爲你的#1重寫規則做相反的事情:如果沒有「www」,然後添加它。


請儘量使用RewriteLog指令:它可以幫助您追蹤到這樣的問題:

# Trace: 
# (!) file gets big quickly, remove in prod environments: 
RewriteLog "/web/logs/mywebsite.rewrite.log" 
RewriteLogLevel 9 
RewriteEngine On 

告訴我,如果它的工作原理。

+1

但是,您是否在引用非ssl內容(即腳本,圖像等)時遇到了問題?我試過這個,但後來我的腳本/樣式表在這些SSL頁面上被引用爲非ssl,它向用戶顯示關於不安全內容的警告消息。 – jon 2011-11-19 17:04:18

+0

+1這對我有用。我做了一個改變而不是'domain.com',我寫了'%{HTTP_HOST}' – diEcho 2012-09-06 08:57:28

相關問題