Redirecting Subdirectory to Subdomain with htaccess
Asked Answered
O

5

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!

Overscore answered 13/9, 2012 at 22:49 Comment(2)
You are adding a www to the beginning of your blog address? So it's turning into http://www.blog.example.com/?Gargle
No, it only adds "www." when the address starts with "example.com"Overscore
N
7

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]
Nunci answered 14/9, 2012 at 9:14 Comment(5)
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.Overscore
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?Overscore
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)Nunci
i need code that redirects www.example.com/blog to blog.example.com. is that what the code above does?Overscore
I don't understand why RewriteRule ^(.*)$ /blog/$1 is used. That looks to be to send the traffic back to subdirectory.Garmon
B
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).

Boz answered 19/8, 2014 at 9:18 Comment(2)
Why would you use a 302?Indorse
Guess only if temporary, 301 probably betterBoz
M
1

Have you tried this one?

RewriteEngine on
RewriteBase /
RewriteRule ^/blog/(.*)$ http://blog.subdomain.com/$1 [R=301,L]
Morganica answered 21/11, 2013 at 19:52 Comment(0)
W
0

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

RewriteCond %{REQUEST_URI} !^/check/

RewriteRule (.*) /check/$1

Waistcoat answered 24/3, 2014 at 5:54 Comment(0)
H
-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]
Hillari answered 14/11, 2020 at 8:22 Comment(1)
This is the opposite of what the OP askedVictorvictoria

© 2022 - 2024 — McMap. All rights reserved.