View Full Version : Redirect depending on domain name

08-22-2010, 08:17 PM
I have multiple domain names, all that lead to the same index page on the root of the server. I want to be able to redirect sitename1.com to sitename1.com/sitename1 and sitename2.com to sitename2.com/sitename2,
etc. I've done this before, but after much searching I still cannot find the solution. I want to do this using Javascript, not PHP or anything else. Thanks in Advance.

08-23-2010, 02:23 AM
Why not do this with the DNS or at least .htaccess? If you must use js it will only work for users with js enabled and it will take some time after the page has loaded to redirect. But it's not too hard: check the address in the URL bar (document.location) and redirect based on that value using if statements.

08-23-2010, 05:44 PM
Unfortunately - I have no control over the DNS except what IP it "forwards" to. But I can use .htaccess, so I'll post my findings.

08-23-2010, 06:48 PM
Yay! I got it to work so that it even hides the change in directory. Here's the code:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^site1.com$ [NC]
RewriteCond $0 !^directory1/
RewriteRule .* directory1/$0 [L]
RewriteCond %{HTTP_HOST} ^site2.com$ [NC]
RewriteCond $0 !^directory2/
RewriteRule .* directory2/$0 [L]

Replace site*.com and directory*/ for all of those and your ready to go! You can also pile as many of those as you want on top of each other (I think).

08-24-2010, 12:12 AM
The L makes it a "last" rule so yes you can make many in a sequence. Glad it works. Note that this will not allow other.com/mypage because any page on other.com will go to the redirect. Remove the ^ if you want to match only the exact domain. And maybe add a rule for www and not www or any other subsomains.