You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Viv D-F <vi...@gmail.com> on 2008/02/29 08:09:06 UTC

[users@httpd] mod_rewrite redirecting multiple domain names to the one name

I have a web server set up with 3 different domain names registered.  For
simplicity, I'll call them...
http://site1.com
http://site2.com
http://site3.com

I want http://site1.com to be the name people remember, so no matter which
URL they use to get to the site, they are redirected to http://site1.com.

Here's a quick excerpt of the httpd.conf file on attempt #1:
LoadModule rewrite_module modules/mod_rewrite.so
ServerName site1.com
RewriteEngine on
#RewriteCond %{HTTP_HOST} !^site1\.com [NC]   #same result with or without
this line
RewriteRule /test.html$ http://site1.com/test.html [R=301,L]

Intended result...
Enter: http://site2.com/test.html
Redirected to: http://site1.com/test.html

If I change the rewrite rule to redirect to http://www.google.com, it
redirects there successfully.  So it seems that no matter what hostname is
used in the request, Apache thinks the http_host is site1.com.  And yes,
unfortunately I do need to redirect back to the same page (test.html) in
this instance.


I figure this is a fairly common problem, but I haven't been able to find
any examples of how this is usually done.  I'm not convinced that this is
best done in the Apache config.  Is this done at the DNS instead?

If you need any more information, please let me know.  Any assistance will
be greatly appreciated.
Thanks.

-Viv-

Re: [users@httpd] mod_rewrite redirecting multiple domain names to the one name

Posted by Joshua Slive <jo...@slive.ca>.
On Fri, Feb 29, 2008 at 2:28 AM, Nils Jeppe <ni...@pandemonium.de> wrote:
>
>
>  On Fri, 29 Feb 2008, Viv D-F wrote:
>
>  > I figure this is a fairly common problem, but I haven't been able to find
>  > any examples of how this is usually done.  I'm not convinced that this is
>  > best done in the Apache config.  Is this done at the DNS instead?
>
>  There are, I think, three ways to do this. The first is what you're
>  thinking of, find an example here:
>
>  http://httpd.apache.org/docs/2.2/rewrite/rewrite_guide.html#canonicalhost
>
>  Secondly, setting "UseCanonicalName on" may work to redirect you to the
>  correct hostname. I never use this myself, but I remember vaguely that it
>  does something like this. I am not entirely sure of course, but you could
>  easily test it yourself:

No, that won't work. (I've always though it would be nice to have a
"UseCanonicalName enforce" which would do exactly that, but it doesn't
exist.)

>  http://httpd.apache.org/docs/2.2/mod/core.html#usecanonicalname
>
>  Thirdly, you could simply add one or more extra virtualhost(s) which takes
>  care of all the "extra" hostnames and do a simple RedirectPermanent /
>  http://site1.com/ or something similar in its configuration. This last
>  solution is especially useful if you'd like to easily log the access /
>  referrers to the "unwanted" site names.

Right. Both of your solutions are discussed in detail here:
http://wiki.apache.org/httpd/CanonicalHostNames

Joshua.

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] mod_rewrite redirecting multiple domain names to the one name

Posted by Nils Jeppe <ni...@pandemonium.de>.

On Fri, 29 Feb 2008, Viv D-F wrote:

> I figure this is a fairly common problem, but I haven't been able to find
> any examples of how this is usually done.  I'm not convinced that this is
> best done in the Apache config.  Is this done at the DNS instead?

There are, I think, three ways to do this. The first is what you're 
thinking of, find an example here:

http://httpd.apache.org/docs/2.2/rewrite/rewrite_guide.html#canonicalhost

Secondly, setting "UseCanonicalName on" may work to redirect you to the 
correct hostname. I never use this myself, but I remember vaguely that it 
does something like this. I am not entirely sure of course, but you could 
easily test it yourself:

http://httpd.apache.org/docs/2.2/mod/core.html#usecanonicalname

Thirdly, you could simply add one or more extra virtualhost(s) which takes 
care of all the "extra" hostnames and do a simple RedirectPermanent / 
http://site1.com/ or something similar in its configuration. This last 
solution is especially useful if you'd like to easily log the access / 
referrers to the "unwanted" site names.



Best wishes
- Nils


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org