2/13/2008

Redirecting specific files and folders from one domain to another with .htaccess

We needed redirection from the old server to the new one with the filenames preserved.

Example

Redirect: http://www.socengine.com/seo/somefile.php
To: http://www.strongwind89.com/somefile.php

Solution

Add the following directive:
RedirectMatch 301 /seo/(.*) http://www.strongwind89.com/$1

Explanation:

The regular expression /seo/(.*) tells apache to match the seo folder followed by zero or more of any character. Surrounding the .* in parenthesis tells apache to save the matched string as a back-reference. This back-reference is placed at the end of the url we are directing to, in our case, $1.
Redirecting without preserving the filename:

Several files that existed on the old server were no long present on the new server. Instead of preserving the file names in the redirection (which would result in a 404 not found error on the new server), the old files just needed to be redirected to the root URL of the new domain.

Redirect: http://www.socengine.com/seo/someoldfile.php
To: http://www.strongwind89.com/

Solution:

Add the following directive:
RedirectMatch 301 /seo/someoldfile.php http://www.strongwind89.com

Explanation:

Omitting any parenthesis, all requests for /seo/someoldfile.php should redirect to the root URL of http://www.strongwind89.com

No comments:

Live Page Popularity