denogginizer
denogginizer

Reputation: 13

Directory to subdomain with same name

Thanks for reading. I have done a search, read multiple posts (lost count) and still having trouble with something that seems simple. I am trying to redirect or rewrite from:

http://www.mysite.com/blog/

to

http://www.blog.mysite.com/

Firstly, should this go in the htaccess file in the root directory or in the blog subdirectory? Secondly, a few of my attempts are below:

(Attempt 1)

RewriteCond %{HTTP_HOST} ^mysite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$
RewriteRule ^blog\/?$ "http\:\/\/www\.blog\.mysite\.com\/" [R=301,L]

(Attempt 2)

RewriteCond %{HTTP_HOST} ^(www\.)?blog\.mysite\.com$
RewriteCond %{REQUEST_URI} mysite.com/blog
RewriteRule ^(.*)$ /blog/$1 [L]

(Attempt 3)

RewriteRule http://mysite.com/blog http://www.blog.mysite.com/$1 [R=301,L]

(Attempt 4)

rewriterule ^blog/(.*)$ http://www.blog.mysite.com/$1 [r=301,nc]

Any help is greatly appreciated.

Upvotes: 1

Views: 395

Answers (2)

user3668009
user3668009

Reputation: 1

A simple code can do this. Use

Redirect /blog http://blog.mysite.com/

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270617

Your first attempt looks nearly correct. I'm going to remove the OR condition and pack both those into a single regex, and remove all the escaped slashes and quotes from the rewrite target:

# Also, are you missing RewriteEngine On?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
# Redirect and place everything after /blog onto /
RewriteRule ^blog(.*)$ http://www.blog.mysite.com$1 [R=301,L]

This should be placed in the root directory's .htaccess.

Upvotes: 1

Related Questions