htaccess redirect examples for SEO's

SEO’s are often tasked with redirecting pages, directories or entire websites, normally with a htaccess file located in the root directory of your website. A htaccess file is a hidden configuration file that Apache reads, the purpose of this file is to allow site configuration at a user level. If you have access to your httpd.conf (Apache configuration file) then you should configure your redirects directly and not use a .htaccess file as they negatively affect website performance.

The following article is designed as a reference for htaccess redirects, in most cases you can copy and paste the code snippet and replace the example domain with your own.

Htaccess Redirect 301

See the example below to redirect a single page (old to new), this is performed with a basic pattern match redirect so the same syntax applies for files or directories.

Redirect 301 Examples

How to redirect a page or directory:

Redirect 301 /old-page-name http://www.your-domain.com/new-page-name

 

To redirect an entire site to a new domain:

Redirect 301 / http://www.new-domain.com

 

Note: The URL structure on the new site would have to be the same as on the old or you will encounter 404 errors.

Redirect Any URL’s Matching a Phrase Using RedirectMatch

The only time you should use a ReWrite rule for redirection are in these circumstances:

  • If you are redirecting based on the query string
  • If you are redirecting based on non URL specific data such as the user agent or IP address

If you need to redirect any URL’s matching a phrase you can use RedirectMatch to perform a regular expression redirect.

RedirectMatch Examples

Redirect an entire directory, any files in /old-dir/ will be redirected to /blog/old-dir/:

RedirectMatch 301 /old-dir/(.*) http://www.yourdomain.com/blog/old-dir/$1

 

Redirect all .gif files to .jpg files on your server.

RedirectMatch (.*)\.gif$ http://www.yourdomain.com$1.jpg

 

301 Redirect Using ReWrite Rules

Here are some ReWrite rule examples for circumstances when other solutions won’t work, but bare in mind the more complex your rules get the more of a performance hog they will become.

Enable Mod_Rewrite

Make sure you have Mod_Write enabled by adding the following line to the top of your .htaccess file:

RewriteEngine on

 

To redirect example.com to the www.example.com

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

ReWrite Rule Redirect Based on UserAgent

RewriteCond %{HTTP_USER_AGENT} UserAgent
RewriteRule .* - [F,L]

 

301 Redirect Based on IP Address:

The following performs a 301 redirect based on the clients IP address, this could be used for banning IP addresses.

Banning an IP Address using ReWrite:

RewriteCond %{REMOTE_ADDR} ^(A\.B\.C\.D)$
RewriteRule ^/* http://www.yourdomain.com/access-denied.html [L]

 

A better option might be to ban the IP address using iptables or use the Auth “Require not ip xxx.xxx.xxx.xxx” directive, use the above only if you wish to redirect the user to a specific page based on their IP address.