Forum Moderators: phranque

Message Too Old, No Replies

Redirecting of Subdomain

         

maange

12:00 pm on Nov 15, 2003 (gmt 0)

10+ Year Member



Hello

I'm pretty new to mod_rewrite and I don't get it work the way I would like it...

What I want to do:
[a.xyz.com...] should be redirected to [xyz.com...] where 'something' can be nothing, a file, a directory, many directories, directories followed by a file name, ...

What I have so far (in httpd.conf) that is not working:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.xyz\.com
RewriteRule ^(.*) [xyz.com...]

I don't know how I can put 'something' into it and how the rest has to look that it works. What did I do wrong?

I'm running an Apache on a Debian-Box.

Where in httpd.conf does this have to be? Right now it's just before 'Section 3, Virtual Hosts'.

Thanks for any help.

P.S.: Sorry for my not so proper English, it's not my mother-tongue...

closed

4:14 am on Nov 16, 2003 (gmt 0)

10+ Year Member



Welcome to WebmasterWorld, maange!

Looks pretty good so far.

To access the contents of buffer 1 in the RewriteRule, you'd have to use $1. Hope that helps.

Your mod_rewrite commands should work fine where they currently are.

maange

1:26 pm on Nov 16, 2003 (gmt 0)

10+ Year Member



It doesn't work. I changed the % to $ and added a line to exclude www.xyz.com. (Somewhere I read about this.)

That's how it looks right now:

<IfModule mod_rewrite>
RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.xyz\.com
RewriteCond %{HTTP_HOST} ^(.*)\.xyz\.com
RewriteRule ^(.*) [xyz.com...]
</IfModule>

What happens now is that [a.xyz.com...] gets redirected to [xyz.com...] ...

Don't I have to add anything to the lines above for 'something' (--> see my first posting).

thanks for any help.

closed

1:59 pm on Nov 16, 2003 (gmt 0)

10+ Year Member



The extra RewriteCond was a nice addition.

Don't I have to add anything to the lines above for 'something' (--> see my first posting).

Oh. That's what I meant. You should have added $1 to the address you want to redirect to.

%1 holds the result for the pattern match in the RewriteCond, while $1 holds the result for the pattern match in the RewriteRule. So when you try to access http://a.xyz.com/something with your code, here's what the buffers would contain:

%1: a
$1: /something

maange

6:46 pm on Nov 16, 2003 (gmt 0)

10+ Year Member



I don't get it - sorry :-(

I chenged my config to:

<IfModule mod_rewrite>
RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.xyz\.com
RewriteCond %{HTTP_HOST} ^(.*)\.xyz\.com
RewriteRule ^(.*) [xyz.com...]
</IfModule>

I still get [xyz.com...] when I access [a.xyz.com......]

What do I do wrong?

jdMorgan

8:57 pm on Nov 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



maange,

Your code is essentially correct.


<IfModule mod_rewrite>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.xyz\.com
RewriteCond %{HTTP_HOST} ^(.*)\.xyz\.com
RewriteRule [b]^/(.*)[/b] https://www.xyz.com/%1/$1
</IfModule>

That should work.

Jim

maange

9:13 pm on Nov 16, 2003 (gmt 0)

10+ Year Member



Hello again,

I added the slash you suggested, but it's still not working :-(

The rewrite module gets loaded. Is there something else important I may forgot?

Thanks!

closed

4:26 pm on Nov 17, 2003 (gmt 0)

10+ Year Member



You did remember to restart the server, right?

Do you have any .htaccess files that override your rewrite rules?

maange

7:51 pm on Nov 17, 2003 (gmt 0)

10+ Year Member



Yes, I did restart the server and no, there is no .htaccess file...

One strange thing I just noticed: when I add the lines
RewriteLog /tmp/rewrite.log
RewriteLogLevel 10

no log gets created when I access [a.xyz.com...]

claus

12:17 pm on Nov 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is this solved? It's perhaps possible to use the example "Virtual User Hosts" from the Apache 1.3 URL rewriteguide: [httpd.apache.org...] (More recent version here: [engelschall.com...]

The example goes like this:
----------------------------------------------

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

----------------------------------------------

I think this might be a way to do it (as an internal redirect), feel free to correct any errors if you spot them:

----------------------------------------------

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?[^.]+\.xyz\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^(www\.)?([^.]+)\.xyz\.com(.*) /$1$2 [L]

----------------------------------------------

If you want an external 301 rewrite the last line should be modified:

----------------------------------------------

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?[^.]+\.xyz\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^(www\.)?([^.]+)\.xyz\.com(.*) http://xyz.com/$1$2 [R=301,L]

----------------------------------------------

What it does (or is supposed to do) is this:

  1. receive request for "http://a.xyz.com/file.htm"
  2. rewrite to "http://a.xyz.com/a.xyz.com/file.htm" AND (flag C for chain)
  3. rewrite to "http://xyz.com/a/file.htm"

As it is now, it also accepts requests like this: "http://www.a.xyz.com/file.htm"

If the "www." is not relevant, just delete this part of the expressions "RewriteCond" and "rewriteRule" above: "(www\.)?"

Hope this helps.
/claus