2011-08-03 54 views
0

我目前正在使用這個.htaccess將帶有目錄的頁面的所有請求重定向到我的index.php強制ssl與.htaccess重寫地址

RewriteEngine on 
RewriteCond $1 !^(index\.php|cas) 
RewriteRule ^(.*)$ /seattle/index.php/$1 [L] 

這樣可以正常工作並生成隱藏index.php的URL,並且我有代碼index.php讓網址看起來乾淨利落。

但是現在我需要強制頁面通過ssl連接,所以我嘗試了

RewriteEngine on 
RewriteCond %{SERVER_PORT} 80 
RewriteCond $1 !^(index\.php|cas) 
RewriteRule ^(.*)$ https://example.com/seattle/index.php/$1 [L] 

它可以強制ssl,但現在它也強制url包含index.php:

https://example.com/seattle/index.php/pagename 

而不是我想要的

https://example.com/seattle/pagename 

我錯過了什麼?

回答

1

要更改協議(HTTP - > HTTPS)和/或域名(www.example.com - >example.com),需要正確的重定向(「301永久重定向」或「302找到/臨時重定向」)。

因此,您不能合併重寫和重定向,仍然顯示原始URL。它必須是2個不同的規則,並且應該首先列出用於更改協議/域的規則。例如:

RewriteEngine on 

# force HTTPS for all URLs 
RewriteCond %{HTTPS} =off 
RewriteRule . https://example.com%{REQUEST_URI} [R=301,L] 

# other rewrite rules 
RewriteCond $1 !^(index\.php|cas) 
RewriteRule ^(.*)$ /seattle/index.php/$1 [L] 

我已經加入該規則將ALL HTTP網址重定向至HTTPS。如果您只需要重定向其中的一部分 - 通過額外的RewriteCond行添加適當的條件。

%{HTTPS}是檢查SSL開啓或關閉(但全部取決於您的具體情況和服務器配置)的最常見和最「合適」的方式。當針對%{HTTPS}進行檢查時,當您的站點在非標準端口(80以外)上運行時,您可以避免出現這種情況。您可以使用%{SERVER_PORT} =80(將適用於大多數情況)。

上述規則將發生在2個步驟http://example.com/seattle/pagename重寫:

  1. 301重定向到https://example.com/seattle/pagename
  2. 重寫(內部重定向)到/seattle/index.php/pagename
+0

+1美麗,你搖滾! –