Using RedirectMatch in .htaccess to redirect entire web site

March 5, 2008 – 9:50 pm
by 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…



Rate this article:


(4 votes, average: 4.25 out of 5)
Loading ... Loading ...

Advertisement:




  1. 3 Responses to “Using RedirectMatch in .htaccess to redirect entire web site”

  2. On Aug 10, 2008 Mister gibson said:

    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..

  3. On Jun 1, 2009 Andrius (nejau.net) said:

    It’s work:)

    Thank You:)

  4. On Aug 24, 2009 Brian Wringer said:

    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!

Post a Comment