6

I'm relatively new to using .htaccess, and have never done any coding besides what I've read online. I'm using Bluehost, and I'd like to redirect my blog subdirectory to a subdomain. Example: I'd like to redirect www.example.com/blog to blog.example.com.

I already have code in place to always add www. to the beginning of my blog address in the root folder, but I don't know how to accomplish the above redirect by using code in .htaccess. Any help would be appreciated!

2
  • You are adding a www to the beginning of your blog address? So it's turning into http://www.blog.example.com/? Commented Sep 13, 2012 at 22:55
  • No, it only adds "www." when the address starts with "example.com" Commented Sep 22, 2012 at 5:28

5 Answers 5

8

A lot of web hosts today provide an easy implemention for subdomain creation in their administration panels. You just need to to go there, choose you subdomain name, and then point it to a directory in your tree.

If you can't, then it will be a little more complicated (You will need to resolve that subdomain to your server ip, configure some virtual hosts ... etc) and you may not have enough privileges to do that (unless you are on a dedicated server).

Edit 2

To redirect requests to www.example.com/blog to blog.example.com, try this :

RewriteEngine on RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ RewriteRule ^blog/(.*)$ http://blog.example.com/$1 [L,QSA,R=301] RewriteCond %{HTTP_HOST} ^blog\.example\.com$ RewriteCond %{REQUEST_URI} !^blog/ RewriteRule ^(.*)$ /blog/$1 [L,QSA] 
Sign up to request clarification or add additional context in comments.

5 Comments

I have already created/setup the subdomain and it works properly at blog.example.com. I simply want to know how to redirect www.example.com/blog, which currently just shows my homepage, to blog.example.com using htaccess code.
Does this code go in the htacess file for my homepage (www.example.com) or in the htaccess file for my subfolder (www.example.com/blog)? Just to eliminate any extra variables, I deleted the bit of code that automatically adds "www." on the front of my site and added the code you suggested above, but it didn't work. "www.example.com/blog" just takes me to my homepage, but the URL doesn't change to the homepage URL - it stays "www.example.com/blog". Any thoughts?
that code goes in your homepage not blog directory, and it is not used for redirecting www.example.com/blog to blog.example.com, it is used for redirecting requests in blog.example.com to the blog directory (see my edited answer)
i need code that redirects www.example.com/blog to blog.example.com. is that what the code above does?
I don't understand why RewriteRule ^(.*)$ /blog/$1 is used. That looks to be to send the traffic back to subdirectory.
2

Have you tried this one?

RewriteEngine on RewriteBase / RewriteRule ^/blog/(.*)$ http://blog.subdomain.com/$1 [R=301,L] 

Comments

2

I wanted to add my two cents,

1) to answer the question above, this rewrite should fix it:

RewriteEngine on RewriteCond %{REQUEST_URI} !\. RewriteRule ^/blog$ http://blog.example.com [R=302,L] 

2) but, I think this is not enough by itself, you also need to change DNS, so that blog.example.com is pointed at the right server, and this can be done by a cname similar to this:

blog.example.com CNAME example.com TTL 1080 

(not exactly how it will look, but use your DNS webinterface to set this up).

2 Comments

Why would you use a 302?
Guess only if temporary, 301 probably better
0

RewriteCond %{HTTP_HOST} ^check.domain.info$

RewriteCond %{REQUEST_URI} !^/check/

RewriteRule (.*) /check/$1

Comments

-1

To redirect subdomain1 and subdomain2 and directory3 to a directory with HTTPS://, I use the following code:

RewriteEngine on RewriteCond %{HTTP_HOST} ^subdomain1.example.com [OR] RewriteCond %{HTTP_HOST} ^subdomain2.example.com [OR] RewriteCond %{HTTP_HOST} ^example\.com/subdomain3 [NC] RewriteRule ^(.*)$ https://example.com/subdirectory/$1 [R=301,L] 

1 Comment

This is the opposite of what the OP asked

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.