Forum Moderators: phranque
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...
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.
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
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?
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...]
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:
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