1
votes

I need to write my htaccess to do the following.

1) I want to redirect example.com/blog/whatever into blog.example.com/whatever (NO HTTPS)

2) example.com --> https://www.example.com (HTTPS and add WWW)

The following is my htaccess. The blog part works fine when it is uncommented, but when I uncomment those two lines related to blog, the www.mydomain.com does not work anymore and I get nothing in the browser, not even 404. If I comment those two line (like it is now) then everything works fine but not the blog part.

Can someone please help me to figure this out so I can have both rules work at the same time.

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]

So basically the problem is when I have these two rules together (blog part and force https) they don't work together, however each individually work if I uncomment the other one.

1

1 Answers

1
votes

This should do it for you.

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]