Using RedirectMatch in .htaccess to redirect entire web site
March 5, 2008 – 9:50 pmby Jake D
When changing websites, I was trying to use Apache’s RedirectMatch to send all documents from one domain to another domain. I used code that I found on other websites, but I found that it didn’t do what I wanted.
What I wanted was for a user to put in http://www.olddomain.com/a_file.html and be redirected to http://newdomain.com/a_file.html. I wanted it to do that for each file, and I didn’t want to specify a redirect for each file. I didn’t want to use mod_rewrite because I wanted to give the 301 redirect for Search Engine Optimization (SEO) reasons.
Quick Answer: use RedirectMatch permanent ^(.*)$ http://www.newdomain.com$1 in a .htaccess file in your old domain.
Initially, I read that this line
RedirectMatch permanent ^(.*)$ http://www.newdomain.com
would redirect all files from the old domain to files on the new domain of the same name when placed in the .htaccess file in the /www/ directory of the old domain. However, when I tried this, all of my redirects simply redirected to the index file of the new domain. For example,
http://www.olddomain.com/file_a.html
http://www.olddomain.com/file_b.html
would both redirect to
http://www.newdomain.com/index.html
Then I modified the apache directive to
RedirectMatch permanent ^(.*)$ http://www.newdomain.com$1
and this worked for me. Even when I put in a bad filename, I got my new domain’s 404 error document.
Not sure why I found RedirectMatch permanent ^(.*)$ http://www.newdomain.com in several sources and it didn’t work…



3 Responses to “Using RedirectMatch in .htaccess to redirect entire web site”
absolutely brilliant, i have been searching ages for a solution to my problem which was the same as yours, i tried this and it worked just fine, thanks
Mick..
It’s work:)
Thank You:)
Finally! I don’t know why .htaccess is so dang mysterious, but I also tried several “solutions” that didn’t work. Thanks for posting your story and the elegant, compact solution. The solution that actually WORKS!