Forum Moderators: phranque

Message Too Old, No Replies

How to redirect all the URL from a set of subdomains tu another (newb)

Redirecting a complete domain (subdomains and URL) to another domain

         

txen

11:14 pm on Apr 30, 2004 (gmt 0)

10+ Year Member



What I want to know is what I would write in my htaccess to redirect ALL the domain A to B (subdomains included). I mean:

The domain: [a.b.c...]
Redirects to: [a.z.c...]

I really know how make that for only one domain but not for a set of subdomains...

It is a heavy and complex code for me to write a RULE for each subdomain, what I'm looking for is only a generic little piece of code. :) thx

jdMorgan

11:27 pm on Apr 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



txen,

Welcome to WebmasterWorld [webmasterworld.com]!

That's a pretty simple application... Have you reviewed the Apache [httpd.apache.org] mod_rewrite and mod_alias documentation? See also the Apache URL Rewriting Guide, linked from the mod_rewrite page.

(By way of explaining this answer, our charter [webmasterworld.com] specifies, that we'll help you get your code working, but not write it for you.)

Jim

txen

9:36 am on May 4, 2004 (gmt 0)

10+ Year Member



Thanks for the bienvenue... :D

Now I show you the code I'm working on:

RewriteEngine on
RewriteCond %{HTTP_HOST} [^.]+\.b\.com$
RewriteRule ([^.]+)\.b\.com(.*) [$1.z.com...]

the problem is that I'm not able to make it work.

I understand that it's a very simple code (I thought that) but my 'newb skill' doesn't let me see the error.
If DNS are ok, what's wrong?

jdMorgan

3:41 pm on May 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The hostname and the URL-path must be treated separately. The hostname is handled by your RewriteCond. So, leave that part of the URL out of your RewriteRule pattern.

If the URL http://example.com/test/code.html is requested, RewriteRule only "sees" the "test/code/html" in .htaccess, or "/test/code.html" in httpd.conf. It does not see the "example.com" part.

RewriteCond {HTTP_HOST} sees only the "example.com" part, plus any appended port number.

Jim

txen

10:37 pm on May 5, 2004 (gmt 0)

10+ Year Member



Here there is an example from the Apache URL Rewriting Guide (Virtual User Hosts example):

1 RewriteEngine on
2 RewriteCond %{HTTP_HOST} ^www\.[^.]+\.host\.com$
3 RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
4 RewriteRule ^www\.([^.]+)\.host\.com(.*) /home/$1$2

that is exactly what I'm looking for but without 'www'. I know that code works fine, and I know that if I'm not able to make it
work it's due to my inexperience and understanding of it.

While I analize the code there is no problem until I arrive at line 3. If the hostname and the URl-path must be treated separately
why the line 3 mixes them?

After a lot of think ( and read what [C] means, well I'm newb ) I have reached a conclusion.

the line 2: I write 'www.a.b.com/c' in my window (there is a match)
the line 3: mod-rewrite redirects 'www.a.b.com/c' to 'www.a.b.com/www.a.b.com/c'
the line 4: redirects 'www.a.b.com/c' to '/home/a/c'

Please, If I'm wrong tell me why, and if I'm right tell me too because I'll continue thinking why my new code doesn't still want
to work on my server.

Txen.

jdMorgan

11:18 pm on May 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> While I analize the code there is no problem until I arrive at line 3. If the hostname and the URl-path must be treated separately why the line 3 mixes them?

HTTP_HOST is not part of the requested URL as seen by RewriteRule, unless you force the hostname into it, as is done in line 3 above. Line 4 then uses the hostname added by line 3.

The solution you have found is more complicated than what is needed.
To redirect all files in domain a.b.c to the like-named files in domain a.z.c: http://a.b.c/d -> http://a.z.c/d, all that is required is a simple two-line ruleset:


RewriteCond ${HTTP_HOST} ^([^.]+\.)?[b]b[/b]\.([^:]+)$
RewriteRule (.*) http://%1[b]z[/b].%2/$1 [R=301,L]

If "c" in your example is always ".com", then you can simplify it even more:

RewriteCond ${HTTP_HOST} ^([^.]+\.)?b\.com
RewriteRule (.*) http://%1z.com/$1 [R=301,L]

The code above is intended for use in .htaccess, and will need slight modifications for use in httpd.conf.

Jim

gergoe

11:22 pm on May 5, 2004 (gmt 0)

10+ Year Member



only some theological problems: there's no redirection done, only rewriting the path of the requested resource, and you should not mention hostnames while talking about RewriteRules because it got nothing to do with.

by the way i don't think this is what you have been looking for since this is about virtual hosting of *different* websites, not about the aliasing of one website.

have fun