2014-10-30 102 views
0

現在我有這樣的代碼重定向非www到www以https不包括CDN及其他子域

RewriteEngine On 
RewriteBase/

# remove the .html extension 
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP 
RewriteRule (.*)\.html$ $1 [R=301] 

# remove index and reference the directory 
RewriteRule (.*)/index$ $1/ [R=301] 

# remove trailing slash if not a directory 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} /$ 
RewriteRule (.*)/ $1 [R=301] 

# forward request to html file, **but don't redirect (bot friendly)** 
RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteCond %{REQUEST_URI} !/$ 
RewriteRule (.*) $1\.html [L] 

#First rewrite any request to the wrong domain to use the correct one (here www.) 
RewriteCond %{HTTP_HOST} !^www\. 
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

#Now, rewrite to HTTPS: 
RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

其工作完全正常,但它是從cdn.domain.com/img重定向我們的CDN圖像。 JPG到www.domain.com/img.jpg我如何防止這種情況發生?

回答

0

可以防止這種通過與cdn.條件測試和沒有替代

RewriteCond %{HTTP_HOST} ^cdn\. 
RewriteRule^- [L] 

此規則前綴的規則留下的網址,因爲它是-

- (破折號)

短劃線表示不應該進行替換 (現有的pat h通過未觸及)。當需要應用一個標誌(見下面)時,使用 而不改變 的路徑。

並停止進一步改寫L|last

的[L]標誌導致mod_rewrite的停止處理規則集。

+0

它仍然將cdn.domain.com/url/img.jpg重定向到www.domain.com/url/img.jpg – user1573389 2014-10-31 16:39:03

+0

我在哪裏添加代碼,是否可以在OP中檢查我刷新的代碼。 – user1573389 2014-10-31 16:42:42

+0

當您在開始處放置此規則時,所有針對'cdn.domain.com'的請求都不會被重寫。 – 2014-10-31 17:27:41