Jump to content

How to 301 redirect ALL visits to one domain to another?


Recommended Posts

Say I have a site with the main URL of mysite.com

 

And let's say my hosting company for some strange reason automatically creates a subdomain of adifferentsite.mysite.com which shows the exact same content as what shows at mysite.com

 

How would I write the 301 redirect (in the htaccess) file to say in plain english...

 

If anyone tries to go to any page that has the root domain of adifferentsite.mysite.com (whether it's adifferentsite.mysite.com/content.php or adifferentsite.mysite.com/monkeys.php or whatever), redirect them to mysite.com?

That didn't work (kicking an "internal server error").  My htaccess file already has this info in it...

 

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mysite.com [nc]
rewriterule ^(.*)$ http://www.mysite.com/$1 [r=301,nc]

 

So based on your reply, I added it to be this.  Do you see anything wrong with how I wrote this?  Can you have two separate rules in there? ...

 

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mysite.com [nc]
rewriterule ^(.*)$ http://www.mysite.com/$1 [r=301,nc]

RewriteCond %{HTTP_HOST} !^mysite.com$
RewriteRule ^ - mysite.com%{REQUEST_URI} [L]

Ok, so the 2nd rule is wrong :(.  So how should I write the 2nd rule to say, in plain english...

 

If anyone tries to go to any page that has the root domain of adifferentsite.mysite.com (whether it's adifferentsite.mysite.com/content.php or adifferentsite.mysite.com/monkeys.php or whatever), redirect them to mysite.com?

 

I tried requinix's suggestion but it didn't work (and cags is saying that it is not doing what I intended anyway)

 

Bottom line is that I want to send all non-www requests to the www version. That is the first rule and it works fine.  Now I just need to figure out how to do the 2nd part explained again above, i.e. forwarding any page with that subdomain in it (i.e. adifferentsite.mysite.com) to my main domain (mysite.com)

 

Thanks!

 

It's simple logic from here, you have all the tools required, you just need to make them make sense. In simple English terms, your rules say

 

Rule 1 : if a request is made to mysite.com, forward the request to the same location on www.mysite.com, regardless of the page requested.

Rule 2 : If a request is made from any subdomain of mysite.com forward the request to the same location on mysite.com.

 

It's important to understand that the L flag only stops parsing of the rules on this request to the server. Both rewrite rules cause a redirect, which in essence tells the clients browser to request a different page (thus making it a different request).

 

Your objective seems to be to make any request that's not for the www subdomain, point at the www subdomain. This being the case I'm not sure why you are playing around with two different rules, in English this is a simple singular statement. Redirect all requests that are not for www.mysite.com to the same URI on the www.mysite.com domain. The information should be clear from what you already have. If you have a go, I may provide the solution (or somebody less interested in helping people actually learn something may provide it in the mean time).

 

 

 

 

I'll give it a go, thanks!  And FYI, I just realized i wrote the wrong thing.  If my main domain is mysite.com, then my subdomain (that I don't like and want to direct traffic away from) is actually mysite.adifferentsite.com (NOT adifferentsite.mysite.com).  I don't think that changes anything, so I'll see what happens.

 

If you're wondering why there is that subdomain, I have shared hosting on HostGator and you only have one MAIN domain and a bunch of ADDON domains.  But for some reason, HostGator makes all pages that you put on addon domains also show up via addondomain.maindomain.com. 

 

So if my main domain is mainsite.com, I  might have a completely different site using one of my addon domains at addonsite.com.  But again, the site that shows up at addonsite.com also shows up at addonsite.mainsite.com.

 

I'm sure HostGator has good reason for it, but they should probably make it more obvious that it does this. I just discovered it after months.

 

i'll let you know what happens. :)

Ok, I believe I got this working.  I included my code below. If you see anything wrong with it, please let me know, but it handles both of these situations, so I think I'm in good shape...

 

1.  If someone goes to mysite.com, it will redirect them to www.mysite.com

 

2.  If someone goes to any page in the structure of mysite.adifferentsite.com/anypage.php, it will redirect to www.mysite.com/anypage.php

 

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mysite.adifferentsite.com [nc]
rewriterule ^(.*)$ http://www.mysite.com/$1 [r=301,nc]

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.