Results 1 to 5 of 5

Thread: Redirect depending on domain name

  1. #1
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Redirect depending on domain name

    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.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  4. #4
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yay! I got it to work so that it even hides the change in directory. Here's the code:

    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).

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •