2017-04-02 49 views
1

我使用的是當前的代碼爲example.com的.htaccess錯誤

RewriteEngine On 
RewriteCond %{HTTP_HOST} !^www\. [NC,OR] 
RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] 
RewriteRule^https://www.%1%{REQUEST_URI} [R=301,L,NE] 

它讓你添加forefully https://www或https://如果你已經把萬維網。

這段代碼的問題是,即使我打電話樣亞域ex.example.com

它增加了https://www.ex.example.com

我想是的子域像這樣=>https://ex.example.com

重定向請任何人都可以給我一個有效的代碼?

+0

這是爲什麼標記爲PHP?這與PHP無關。 –

+0

你想讓這個'example.com'到'https:// www.example.com'和它的子域名到'http:// that.subdomain.com'嗎? –

+0

我希望子域名像'https:// that.example.com'而不是'http' –

回答

0

希望這會工作正常。

RewriteEngine On 

#for normal domains like example.com or example.in to redirect to https://www.example.com 
RewriteCond %{HTTP_HOST} !^www\. [NC,OR] 
RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} ^([^\.]+)\.(com|in)$ [NC] 
RewriteRule^https://www.%1.%2%{REQUEST_URI} [R=301,L,NE] 

#for subdomain like test.example.com to redirect to https://test.example.com 
RewriteCond %{HTTP_HOST} !^www\. [NC,OR] 
RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} ^([\.\w\-]*)\.(com|in)$ [NC] 
RewriteRule^https://%1.%2%{REQUEST_URI} [R=301,L,NE] 
+0

它的主域名工作,根據子域它唯一的woking爲mail.test.com不是爲demo.test.com,爲什麼呢? –

+0

它應該爲此工作,並再次檢查它 –

0

要強制HTTPS子域,把下面的右後RewriteEngine敘述上行:

RewriteCond %{HTTPS}#%{HTTP_HOST} ^off#sub\.example\.com$ 
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R] 

這將重定向http://sub.example.comhttps://sub.example.com。您已將此項放在其他HTTPS重定向規則之前,否則您的規則將覆蓋此項。