How-To Redirect All Directories of oldsite to newsite in htaccess

By | 2013/04/30

Here is a quick tip to help redirect an existing url structure from an old domain to a new domain. Scenario is two domains are both pointed at same server and web root. Need to substitute old domain for new domain and keep all existing URL structure in tack.

Ex: -> ->

Here is an example mod_rewrite rule for .htaccess that will do the trick. The resulting redirects are 301 redirects (Moved Permanently).

RewriteEngine  On
RewriteBase   /
RewriteCond   %{HTTP_HOST} ^oldsite\.com [OR]
RewriteCond   %{HTTP_HOST} ^www\.oldsite\.com
RewriteRule   ^(.+[^/])$$1/  [R]

Test with curl:

$ curl -v

* About to connect() to port 80 (#0)
*   Trying
* connected
* Connected to ( port 80 (#0)
> GET /this/directory/ HTTP/1.1
> User-Agent: curl/7.27.0
> Host:
> Accept: */*
* additional stuff not fine transfer.c:1037: 0 0
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 301 Moved Permanently
< Date: Tue, 30 Apr 2013 17:34:31 GMT
< Server: Apache
< Location:
< Content-Length: 295
< Connection: close
< Content-Type: text/html; charset=iso-8859-1

Leave a Reply

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.