So I'm trying to write a mod_rewrite rule that will send everything on my main domain to a subdomain.

For example, redirect

http://example.com/1/2/3/4/5?n=6&i=7

to

http://sub.example.com/1/2/3/4/5?n=6&i=7

Here's what I have so far:

RewriteEngine On
RewriteCond ^http://www\.example.com\/ [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [R=301,L]

But it doesn't seem to working. Any tips?

link|improve this question
@Shane Madden - don't think it's a dupe myself. That linked q is very helpful, but since it doesn't cover this specific question I think it is useful in its own right. – Gavin C May 23 '11 at 22:24
1  
@Gavin Fair point. That question could use a wider breadth of examples, it's a little heavy on referrer examples at the moment. – Shane Madden May 24 '11 at 0:46
feedback

1 Answer

I think you're missing something in your RewriteCond line. Try this:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^http://www\.example.com\/ [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [R=301,L]

So add the %{HTTP_HOST} into your RewriteCond rule... Note that I haven't tested this, so please post the results...

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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