.htaccess redirect of domain name alias to main domain but must show up as the alias domain
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
.
Related videos on Youtube
MSE
Updated on September 18, 2022Comments
-
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 theindex.html
file and show up asaliasdomain.com
.However it shows up as the full URL as follows:
http://www.maindomain.com/subdir1/subdir2/index.html
-
MSE almost 11 yearsOops, thanks for this and I'm sure you are on the right track but I got an "Internal Server Error".
-
MrWhite almost 11 yearsSorry, yes - a second
RewriteCond
directive is required in order to prevent a rewrite loop. I've updated the code. -
MSE almost 11 yearsThanks 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 almost 11 yearsI'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 almost 11 yearsI can access mayo-ireland.ie/Mayo/Towns/Aghamore/Aghamore.htm directly. but not the link you have given.
-
MSE almost 11 yearsYes the message comes up on Firefox. However page doesn't load on Safari or Google Chrome either.
-
MSE almost 11 yearsJust 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 almost 11 yearsThat could well be related. You are using SSI? If
aghamore.com
is a domain alias ofmayo-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 aboveRewriteRule
serves the file fromaghamore.com
. However, it looks as if your site is hard-wired to serve files only frommayo-ireland.ie
? If you remove the above directives and you cannot view your site viaaghamore.com
, then what you are trying to do is not going to work unfortunately. -
MSE almost 11 yearsI'll try and absorb what you have just said :) Thanks for all your time.
-
MSE almost 11 yearsI have removed the directive and tried aghamore.com in the browser and the home page for mayo-ireland.ie comes up OK.
-
MrWhite almost 11 yearsCurious, 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 almost 11 yearsJust 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 almost 11 yearsJust site root.
-
MrWhite almost 11 yearsAh OK, that greatly simplifies the problem. :) I have updated my answer with the new code.
-
MrWhite almost 11 yearsJust to stress, the important change in this new code is the change from
.*
(or^(.*)$
) to^$
in theRewriteRule
pattern. The former matched everything, whilst the new code matches nothing (ie. a request for just the site root). -
MrWhite almost 11 yearsYou'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.