galvin Posted September 12, 2011 Share Posted September 12, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/246994-how-to-301-redirect-all-visits-to-one-domain-to-another/ Share on other sites More sharing options...
requinix Posted September 12, 2011 Share Posted September 12, 2011 With mod_rewrite (thus the move). RewriteCond %{HTTP_HOST} !^whatever.your.site.is.com$ RewriteRule ^ - http://whatever.your.site.is.com%{REQUEST_URI} [L] Quote Link to comment https://forums.phpfreaks.com/topic/246994-how-to-301-redirect-all-visits-to-one-domain-to-another/#findComment-1268504 Share on other sites More sharing options...
galvin Posted September 12, 2011 Author Share Posted September 12, 2011 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] Quote Link to comment https://forums.phpfreaks.com/topic/246994-how-to-301-redirect-all-visits-to-one-domain-to-another/#findComment-1268512 Share on other sites More sharing options...
cags Posted September 12, 2011 Share Posted September 12, 2011 Your first rule redirects mysite.com to www.mysite.com, your second rule redirect www.mysite.com to mysite.com, your first rule redirects mysite.com to www.mysite.com etc. Quote Link to comment https://forums.phpfreaks.com/topic/246994-how-to-301-redirect-all-visits-to-one-domain-to-another/#findComment-1268523 Share on other sites More sharing options...
requinix Posted September 12, 2011 Share Posted September 12, 2011 And there's a mistake in what I wrote: an extra hyphen. Habit RewriteRule ^ http://whatever.your.site.is.com%{REQUEST_URI} [L] Quote Link to comment https://forums.phpfreaks.com/topic/246994-how-to-301-redirect-all-visits-to-one-domain-to-another/#findComment-1268536 Share on other sites More sharing options...
galvin Posted September 13, 2011 Author Share Posted September 13, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/246994-how-to-301-redirect-all-visits-to-one-domain-to-another/#findComment-1268705 Share on other sites More sharing options...
cags Posted September 13, 2011 Share Posted September 13, 2011 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). Quote Link to comment https://forums.phpfreaks.com/topic/246994-how-to-301-redirect-all-visits-to-one-domain-to-another/#findComment-1268730 Share on other sites More sharing options...
galvin Posted September 13, 2011 Author Share Posted September 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246994-how-to-301-redirect-all-visits-to-one-domain-to-another/#findComment-1268785 Share on other sites More sharing options...
galvin Posted September 14, 2011 Author Share Posted September 14, 2011 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] Quote Link to comment https://forums.phpfreaks.com/topic/246994-how-to-301-redirect-all-visits-to-one-domain-to-another/#findComment-1269324 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.