.htaccess redirect of domain name alias to main domain but must show up as the alias domain

5,107

You need to internally rewrite the request in order to keep aliasdomain.com in the browser, and not externally redirect as you are currently doing...

Remove the protocol/host from the RewriteRule substitution, to leave just a root-relative path, and remove the R (redirect) flag. The protocol/host in the substitution forces an external redirect.

RewriteCond %{HTTP_HOST} ^(www\.)?aliasdomain\.com$
RewriteRule ^$ /subdir1/subdir2/index.html [L]

The ^$ pattern in the RewriteRule directive ensures that only requests for the domain root (ie. http://aliasdomain.com/ or http://www.aliasdomain.com/) gets rewritten to the new URL, rather than http://aliasdomain.com/every/possible/url (as mentioned in the comments below).

Since aliasdomain.com is a domain alias of maindomain.com, the entire site is accessible via both domains. They point to the same place. This is what the above rewrite rules are dependent upon. You can't internally rewrite to a different host. So the above RewriteRule serves the file from aliasdomain.com.

Share:
5,107

Related videos on Youtube

MSE
Author by

MSE

Updated on September 18, 2022

Comments

  • MSE
    MSE over 1 year

    I have the following in a .htaccess file:

    RewriteCond %{HTTP_HOST} ^www\.aliasdomain\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^aliasdomain\.com$
    RewriteRule ^(.*)$ http://www.maindomain.com/subdir1/subdir2/index.html [R]
    

    In a browser I want aliasdomain.com to access the index.html file and show up as aliasdomain.com.

    However it shows up as the full URL as follows: http://www.maindomain.com/subdir1/subdir2/index.html

  • MSE
    MSE almost 11 years
    Oops, thanks for this and I'm sure you are on the right track but I got an "Internal Server Error".
  • MrWhite
    MrWhite almost 11 years
    Sorry, yes - a second RewriteCond directive is required in order to prevent a rewrite loop. I've updated the code.
  • MSE
    MSE almost 11 years
    Thanks again. Now getting the following error: Content Encoding Error The page you are trying to view cannot be shown because it uses an invalid or unsupported form of compression.
  • MrWhite
    MrWhite almost 11 years
    I'm pretty sure that error is not related to this current issue. Can you access http://aghamore.com/Mayo/Towns/Aghamore/index.html directly in your browser? Are you using Firefox?
  • MSE
    MSE almost 11 years
    I can access mayo-ireland.ie/Mayo/Towns/Aghamore/Aghamore.htm directly. but not the link you have given.
  • MSE
    MSE almost 11 years
    Yes the message comes up on Firefox. However page doesn't load on Safari or Google Chrome either.
  • MSE
    MSE almost 11 years
    Just found this in Google. Does it help? "I finally found the problem - an Apache virtual include referred to a directory rather than a file. Hopefully it'll help someone else to know this can give the above error. "
  • MrWhite
    MrWhite almost 11 years
    That could well be related. You are using SSI? If aghamore.com is a domain alias of mayo-ireland.ie then all files are accessible from both domains - they point to the same place. This is what the above rewrite rules are dependent upon. You can't internally rewrite to a different host. So the above RewriteRule serves the file from aghamore.com. However, it looks as if your site is hard-wired to serve files only from mayo-ireland.ie? If you remove the above directives and you cannot view your site via aghamore.com, then what you are trying to do is not going to work unfortunately.
  • MSE
    MSE almost 11 years
    I'll try and absorb what you have just said :) Thanks for all your time.
  • MSE
    MSE almost 11 years
    I have removed the directive and tried aghamore.com in the browser and the home page for mayo-ireland.ie comes up OK.
  • MrWhite
    MrWhite almost 11 years
    Curious, what form does your "Apache virtual include" take? Maybe it's being rewritten? I had wondered whether your images, CSS and JS files were also being rewritten (additional directives in the above are required in order to avoid this if you are using relative paths), however, you appear to be using absolute paths to your primary domain(s) for everything that is returned client-side!
  • MrWhite
    MrWhite almost 11 years
    Just rethinking the problem, do you really want to rewrite everything, ie. http://aghamore.com/every/possible/url to the target page? (Which is what this currently does.) Or just requests for the site root, ie. http://aghamore.com/?
  • MSE
    MSE almost 11 years
    Just site root.
  • MrWhite
    MrWhite almost 11 years
    Ah OK, that greatly simplifies the problem. :) I have updated my answer with the new code.
  • MrWhite
    MrWhite almost 11 years
    Just to stress, the important change in this new code is the change from .* (or ^(.*)$) to ^$ in the RewriteRule pattern. The former matched everything, whilst the new code matches nothing (ie. a request for just the site root).
  • MrWhite
    MrWhite almost 11 years
    You're welcome - glad it worked OK for you in the end. If it's resolved then you can mark the answer as accepted (green tick below the up/down votes next to the answer) - this then takes the question out of the unanswered question queue, so others know it has been answered. (You also get some reputation points for accepting an answer.) Unfortunately you need at least 15 rep before you can upvote.