Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I've been hunting around the tubes today, but given my limited mod_rewrite and regular expression knowledge I'm struggling.

What I'm trying to achieve is a rewrite of domain1 (including subdomains) to domain2 (including subdomains) and implemented as a 301. Domain 2 is the primary domain, domain 1 is just (for the time being) set up alongside and really just there because 'it is' (a better use is pending, thought I'd use it to learn something in the meantime).

Domain 2 is configured as wildcard, and hosts microsites that are displayed via subdomain -> subfolder internal rewrite. (will happily post code for this on request, it's not tricky but it did take me a while to get it working without infinite rewrites, for some odd reason). Domain 1 (the temp domain) isn't set up at all, but only because I'm face with several options (a DirectAdmin-implemented 'alias' or 'pointer', or added as a a separate domain and configured as wildcard). If someone goes to domain 1 (the forwarding domain), this should happen:

http://sub.domain1.tld -> http://sub.domain2.tld
http://sub.domain1.tld/content.php -> http://sub.domain2.tld/content.php
http://domain1.tld -> http://domain2.tld
http://domain2.tld

What's bugging me is that all of the solutions readily available on the web either map subdomains to directories, are only coded to cope with requests via http://domain1.tld/. One of my own old htaccesses, for redirecting a .co.uk to a .com, has the beginnings of where I want to go:

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.foo\.com [NC] 
RewriteRule ^(.*)$ http://foo.co.uk/$1 [R=301,L]

I understand that $1 denotes everything after the domain as matched by the ^(.*)$ pattern, and %2 would be the subdomain (so I'm supposing %1 would be "www." if it was present in the http_referer or otherwise null?)

...However, this is where I get stuck. What would be the neatest way of rewriting to carry across any variables to a new domain name - and not losing any subdomains along the way?

share|improve this question
add comment

3 Answers

Is there a reason that Redirect won't work, in the config for domain1:

Redirect permanent / http://domain2.com/

Redirect redirects everything under the LHS to the RHS, keeping the same directory structure.

Alternately, an even simpler solution is to just add ServerAlias domain2 in the config for domain1, although that doesn't send redirects, just makes the server respond to requests for domain2 in the same way as domain1.

share|improve this answer
    
I was under the impression Redirect permanent would handle a site and subdirs, but not subdomains... Or am I Doing It Wrong? –  Christopher Woods Jan 20 '10 at 14:08
    
Hm, I missed the preserving subdomains part. I think you're on the right track with your RewriteCond/RewriteRule example, and it looks like it should work. What's the problem with it? –  Darren Chamberlain Jan 21 '10 at 16:42
add comment

Try something along the lines of (untested):

RewriteMatch 301 http://(.*)domain1.com(.*) http://$1domain2.com$2
share|improve this answer
    
Interesting. If I want to use multiple RedirectMatch rules to look first for any subdomains, and then if none are found look for anything after the hostname, might that work? My goal is to try and fit all of that search behaviour into one line to minimise server load when parsing but I'm struggling a bit with the regex composition (because I'm a noob at it). Do you use or know of any realtime regex validators that are design for use with writing stuff like mod_rewrite rules? I've found general validators, but with no point of reference or guide it's like flailing in the dark sometimes ;) –  Christopher Woods Jan 20 '10 at 14:13
add comment

I think you want something like this:

RewriteCond %{HTTP_HOST} ^((www\.)?[^\.]+)\.foo\.com [NC]
RewriteRule ^(.*)$ http://%1.foo.co.uk/$1 [R=301,L]

That will keep the %1 (the subdomain part of foo.com, including the www if present) and put it into the RHS of the RewriteRule. Keep in mind that these are just strings while mod_rewrite is mangling them, so you can do anything with them.

If the optional www part gives you problems, split it into two RewriteCond statements:

RewriteCond %{HTTP_HOST} ^(www\.[^\.]+)\.foo\.com [NC] [OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.foo\.com [NC]
RewriteRule ^(.*)$ http://%1.foo.co.uk/$1 [R=301,L]
share|improve this answer
    
Hi Darren, I've been playing with this for a while now and for some reason I can't get it to work properly. (this is on CentOS, Apache2, on a DirectAdmin-managed account although I also have root access). Bizarrely, this works: RewriteCond %{HTTP_HOST} ^domain1.co.uk$ [NC] RewriteRule ^(.*)$ domain2.com/$1 [R=301,L] But introducing the www. check makes the rules not evaluate, and fail: RewriteCond %{HTTP_HOST} ^((www\.)?[^\.]+)\.domain1\.co\.uk [NC] RewriteRule ^(.*)$ http://%1.domain2.com/$1 [R=301,L] The same goes for the split statements. I can't understand why... –  Christopher Woods Mar 17 '10 at 11:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.