I have a web server. Lots and lots of DNS entries for domains and subdomains are pointing at this server. Most of them, like www.foo.com, foo.com, and subdomain.foo.com, I want to keep. However, there is a certain list of subdomains for which I want to redirect.

For example, all of these urls resolve to the web server.

test.foo.com bar.foo.com baz.foo.com

test.foo.com is fine, we don't have to do anything, but bar and baz are on the list. That means we have to redirect them to

bar.list.foo.com baz.list.foo.com

I can't just do a subdomain wild-card rewrite here because there is a long list of subdomains we want to redirect and a long list of ones we do not want to redirect.

Can/should I do a mod_rewrite that compares the subdomain to a list in a text file? If so, how do I do that? If not, how do I handle these redirects?

I know that I can make an entry into a mod_rewrite configuration, one for each subdomain, but that is a lot of typing, and there must be a better answer.


You could use the RewriteMap directive to provide a list of subdomains (or other keys) that you can use inside rewrite rules.

  • 1
    I agree... I've used RewriteMaps and they work extremely well. One thing I would be sure to do is to convert the text file once it's working to a berkley db file using the httxt2dbm utility – goose Aug 19 '09 at 16:58
  • I know about ReWriteMap, but it doesn't seem to do what I'm looking for. If it does, please provide an example. – Apreche Aug 19 '09 at 19:19
  • Take a spin through the examples: httpd.apache.org/docs/2.2/rewrite/rewrite_guide.html and httpd.apache.org/docs/2.2/rewrite/rewrite_guide_advanced.html then come back with questions? – CK. Aug 19 '09 at 21:31
  • Take a look at the section "Mass Virtual Hosting". I'm trying in my own server, if my tests are sucessful I will let you know. – hdanniel Aug 20 '09 at 1:48
  • Ok, I was wrong. rewritemap can do what I want. Just all the examples of it I saw were using it in another way. – Apreche Aug 20 '09 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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