Unhappy Blogger
Unhappy Blogger

Reputation: 183

URL Redirect Via HTACCESS

I'm starting a blog on a domain which I have just purchased and would like to set up another blog as a subdomain.

Now if this secondary blog takes off as well as I hope it does in the near future I'd like to set it up on a domain of it's own, but I'm wondering how easy it is to redirect people who have the existing url to the new one.

I have a folder structure like this:

domain.com >>> htaccess . index.php . about.php . blog/ . features/

My Blog and Features folders also contain a htaccess file and their own index page + other relevant php pages.

I was wondering if you could redirect people to the new domain using only the primary htaccess file, which is located under the main domain.com folder, or if you'd have to edit the htaccess within each sub folder.

What would be the best way of doing this, and to some extent, how would this be done successfully?

Upvotes: 1

Views: 77

Answers (2)

BeetleJuice
BeetleJuice

Reputation: 40896

Once you move your subdomain sub.mydomain.com to newdomain.com, you can use htaccess mod_rewrite module to redirect queries from the subdomain to the new domain. You would put the rules in the top level htaccess file that controls the old domain. With the example domains above, it would be something like:

Options +FollowSymLinks
# turn on mod_rewrite
RewriteEngine On
RewriteBase /

# only redirect if query is for the sub domain (non case-sensitive match)
RewriteCond %{HTTP_HOST} ^sub\.mydomain\.com$ [NC]

# rewrite all urls (that match the subdomain host) to new domain, 
# be sure to include the original path and query string
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [QSA,L,R=301]

If your new domain doesn't use www prefix, just remove that from the rewrite rule. QSA tells the server to send along the query string (part of the url after the ? character). L means stop processing htaccess document. R=301 means to tell the browser that from then on it can go directly to newdomain.com whenever the user tries to go to sub.mydomain.com

Upvotes: 0

Joe
Joe

Reputation: 4917

Yes, all you would need to do is use the following:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newblog.com/$1 [R=301,L]

So when someone accesses the old domain, it would direct to the new one.

Upvotes: 1

Related Questions