I have a site at blog.foobar.com that I have closed down, and I want any page requested there to be forwarded to www.foobar.com

I want my VirtualHost config to do this for me. I currently have the following lines that does nearly what I want but not exactly:

redirect permanent / http://www.foobar.com

Unfortunately what happens is that if I ask for blog.foobar.com instead of forwarding to www.foobar.com it serves the pages on blog.foobar.com instead.

Is there a way doing this in the VirtualHost config or should I use a .htaccess file instead?

Regards

Steve

Accepted Answer

You can use the Redirect directive in the context of either a VirtualHost or a .htaccess file. However, what you probably want is a RedirectMatch:

RedirectMatch permanent (.*)$ http://www.foobar.com$1

With that inside your blog.foobar.com VirtualHost, any request to blog.foobar.com would be directed to the same page on www.foobar.com, ie. blog.foobar.com/my/page would go to www.foobar.com/my/page.

Written by zombat
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki