2017-06-21 41 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