How to setup a 301 redirect to another website using .htaccess

A permanent 301 redirect in your .htaccess file lets search engines such as Google and others know that an old link has been replaced by a new one. Having your website accessed with or www is frown upon by search egines like Google. Google penalizes this as it is seen as duplication content. Below are some common use of 301 redirect:

How to redirect or force www. version of domain to be used or you can force non www. version of domain to be used

Visitors access and link to your website in multiple ways such as mydomain.com, and www.mydomain.com and you can set one as the preferred method that your site displays.

Redirect mydomain.com to www.mydomain.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mydomain.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]

Redirect www.mydomain.com to mydomain.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$
RewriteRule ^/?$ "http\:\/\/mydomain\.com\/" [R=301,L]

Redirect an old domain to a new domain

You've moved a website from an old domain to a new one, and you want any old links to go to the new site.

Redirect mydomain.com to www.mydomain.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.mydomain.com [NC]
RewriteRule ^(.*)$ http://mydomain.net/$1 [L,R=301,NC]

Redirect individual page

An old file has moved locations, or the information is now contained in a new file.

Redirect individual page to another on the same domain:
Redirect 301 /oldpage.html /newpage.html

Redirect individual page to another on another domain:
Redirect 301 /oldpage.htm http://mydomain.info/newpage.html

Redirect all files with certain extension

To re-direct all of one type of file to another, such as files or pages with .php extension to another
RewriteEngine On
RewriteCond %{REQUEST_URI} .php$
RewriteRule ^(.*).php$ /$1.htm [R=301,L]

Was this useful? Share it!

UK REGISTERED COMPANY 11074916 |
Copyright © 2019 - Solelyweb Ltd - All Rights Reserved |