2017-06-21 103 views
1

我需要編寫我的htaccess來執行以下操作。htaccess重定向規則衝突與海誓山盟

1)欲example.com/blog/whatever重定向到blog.example.com/whatever(NO HTTPS)

2)example.com - >https://www.example.com(HTTPS和添加WWW)

以下是我的htaccess。博客部分在沒有註釋的情況下工作正常,但是當我取消註釋與博客有關的這兩行時,www.mydomain.com不再工作,我在瀏覽器中什麼都沒有,甚至404也沒有。如果我評論這兩行(像現在這樣),那麼一切正常,但不是博客的一部分。

有人可以幫我解決這個問題,所以我可以同時使用這兩個規則。

RewriteEngine on 

RewriteCond %{SERVER_PORT} 80 

#blog config 

#RewriteRule ^blog$ http://blog.example.com/ [P,L,R=301] 

#RewriteRule ^blog/(.*)$ http://blog.example.com/$1 [P,L,R=301] 


# forward all the request to https and www 

RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L] 


# Framework purposes 

RewriteCond $1 !^(index\.php|assets) 

RewriteRule ^(.*)$ index.php?/$1 [L] 

所以基本上問題是,當我有這兩個規則一起(博客部分和力HTTPS),他們不在一起工作,但是如果每一個我去掉了另外一個獨立工作。

回答

1

這應該爲你做。

RewriteEngine on 

# Redirect blog 
RewriteRule ^blog/(.*)$ http://blog.example.com/$1 [NE,R=301,L] 

# Redirect all HTTPS or non-www requests to HTTPS and www 
RewriteCond %{SERVER_PORT} 80 [OR] 
RewriteCond %{HTTP_HOST} !^www\. 
RewriteRule ^(.*)$ https://www.example.com/$1 [NE,R=301,L] 

# Framework purposes 
RewriteCond $1 !^(index\.php|assets) 
RewriteRule ^(.*)$ index.php?/$1 [L] 
+0

非常感謝SuperDuperApps。你能否向我解釋一下這個區別?我真的很想理解這個問題。 –

+0

還有一個小問題可以幫助我,那就是該協議已經是https,那麼這個代碼不會將www添加到它。我怎樣才能強制WWW的HTTPS? –

+0

是的,我注意到了非www的HTTPS問題,我會爲此添加一個修復程序,然後回覆您,以便更好地理解它。 – SuperDuperApps