2016-09-22 220 views
0

我試圖重定向:的.htaccess重定向非www和非HTTP到https:// WWW

  • http://example.extension
  • https://example.extension
  • http://www.example.extension

  • https://www.example.extension

使用:

RewriteCond %{HTTP_HOST} ^example.extension$ [OR] 
RewriteCond %{HTTPS_HOST} ^example.extension$ [OR] 
RewriteCond %{HTTP_HOST} ^www.example.extension$ [NC] 
RewriteRule (.*) https://www.example.extension$1 [R=301,L] 

http://example.extension被重定向到https://www.example.extension,不過,我收到錯誤:

The page isn’t redirecting properly

this answer,如果我改變.htaccess規則:

RewriteCond %{HTTPS} off [OR] 
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteRule^https://www.example.extension%{REQUEST_URI} [R=301,L,NE] 

即使清除瀏覽器緩存後也會出現相同的症狀。

另外,我有一些子域我不想重定向到www,如:

我需要http://*.example.extension重定向到https://*.example.extension

除了我正在請求幫助的重寫規則外,中的唯一其他內容是WordPress相關:

# BEGIN WordPress 
RewriteEngine On 
RewriteBase/
RewriteRule ^index\.php$ - [L] 

# add a trailing slash to /wp-admin 
RewriteRule ^wp-admin$ wp-admin/ [R=301,L] 

RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule^- [L] 
RewriteRule ^(wp-(content|admin|includes).*) $1 [L] 
RewriteRule ^(.*\.php)$ $1 [L] 
RewriteRule . index.php [L] 
# END WordPress 

# BEGIN MainWP 
# END MainWP 

幫助讚賞。

回答

1

隨着CloudFlare的,你必須使用CloudFlare的頁面規則: https://support.cloudflare.com/hc/en-us/articles/200170536-How-do-I-redirect-all-visitors-to-HTTPS-SSL-

但是你可以使用:

RewriteEngine On 

#main domain 
RewriteCond %{HTTP_HOST} !^www\. [NC,OR] 
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"' 
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.extension)$ [NC] 
RewriteRule^https://www.%1%{REQUEST_URI} [R=301,L,NE] 

#sub-domains 
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"' 
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE] 
+0

嗨Croises。這些規則是否考慮到我擁有的子域?我已經添加了這些規則,取代了現有規則,Firefox說:「Firefox已經檢測到服務器正在以一種永遠不會完成的方式重定向這個地址的請求。 我已經添加了一個Cloudflare頁面規則設置'http:// *。example.extension/*'來始終使用'https',我認爲這很好。 – Steve

+0

我修改了您的子域問題更改的代碼。 – Croises

+0

您可以使用CloudFlare規則完成所有工作,但是避免在將CloudFlare代理重定向到錯誤的域後 – Croises

2

沒有變量HTTPS_HOST

這一規則替換您的規則:

RewriteEngine On 

# main domain: add www and turn on https in same rule 
RewriteCond %{HTTP_HOST} !^www\. [NC,OR] 
RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.extension)$ [NC] 
RewriteRule^https://www.%1%{REQUEST_URI} [R=301,L,NE] 

# sub domain 
RewriteCond %{HTTPS} off 
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE] 

確保徹底清除瀏覽器緩存。

+0

感謝。我用上面的代碼替換了'.htaccess'規則,即使在清除瀏覽器和CDN(Cloud Flare)緩存後,也會出現相同的症狀。 – Steve

+0

我忘了添加我有一些子域需要重定向從'http'到'https'(即我需要'http:// * example.extension'重定向到'https://*.example .extension「也)。 – Steve

+0

這個規則來自一個實時網站,它正在工作。我爲子域添加單獨的規則,但首先測試主域,並確保在測試時沒有其他規則。 – anubhava

相關問題