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

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]

  • 0 Els usuaris han Trobat Això Útil
Ha estat útil la resposta?

Articles Relacionats

How to register or buy a domain name?

How to register or buy a domain name? Step 1: Find a search box similar to the one below. One...

What is a top-level domain?

What is a top-level domain? .com, .org, .net or .uk are all examples of a top-level domain...

What is a registry, registrar and registrant?

There a lot of things that goes on behind the scenes when you buy a domain name. Below we explain...

Can I change the name of my domain?

Can I change the name of my domain? It is not possible to change your domain, after it has...

What is an Addon Domain?

An add-on domain is a separate domain name you add to an existing web Hosting subscription with...