301 redirect for all occasions using .htaccess

April 5, 2016 17 Yehor Rykhnov

301 redirect - permanent redirect, i.e. 301 redirect indicates that the page is moved to the new address and the old URL considered obsolete, such redirect sends about 90-99% weight of the reference pages.

Next will be described a few tips and examples 301 redirect with help .htaccess

Advice

Try to avoid consecutive redirects, because each redirect it is time load of page, transfer of link mass and fuzzy instruction for search robot.

Do not redirect to the page where the answer is different from the code 200. Redirect should lead on to existing page with a 200 server response.

Remember that browsers cache redirects, so check redirect using online services.

301 redirect from one page to another

It is used when a page is moved from one URL to another. For example the old URL of the page /page-1/ you need to do a 301 redirect to the URL http://mysite.com/new-page-1/

Redirect 301 /page-1/ http://mysite.com/new-page-1/

or

RewriteCond %{REQUEST_URI} ^/page-1/$
RewriteRule ^.*$ http://mysite.com/new-page-1/? [R=301,L]

301 redirect from the www domain on without the www

301 redirect with www domain to the non-www domain this called canonization or gluing. For example we do a redirect with http://www.mysite.com to http://mysite.com, primary mirror of site is http://mysite.com

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

or

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

301 redirect to the domain without the www to the www with domain

As in the case described above it is also called canonicalization or gluing of domain. For example a 301 redirect from a domain http://mysite.com to http://www.mysite.com, primary mirror of site is www.mysite.com

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

or

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

301 redirect from the pages with a slash to the page without a slash at the end of the URL

It often happens that the same page is available on two URL, for example /may-best-page and /my-best-page/. If the person understands that this is one and the same page, the search engines are aware of this as two different pages. As a result divide the weight of the page and shown in analytics (statistics) as 2 different pages. To avoid this you can make a 301 redirect from the pages with a slash at the end of the URL to without him.

RewriteCond %{REQUEST_URI} !\?
RewriteCond %{REQUEST_URI} !\&
RewriteCond %{REQUEST_URI} !\=
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} ![^\/]$
RewriteRule ^(.*)\/$ /$1 [R=301,L]

This redirection will work as follows:

URL Redirect
http://mysite/page/ http://mysite/page
http://mysite/page/?value=1 http://mysite/page?value=1
http://mysite/page.html/ http://mysite/page.html
http://mysite/page?value=1/ http://mysite/page?value=1
http://mysite/page without redirect
http://mysite/page.html without redirect
http://mysite/page?value=1 without redirect

301 redirect pages with no slash on the page with a slash in the end of the URL

The reason to make such a redirect is the same as that in the situation described above, except that you need to do a redirect from the page without a slash at the end of a URL to a page with a slash at the end of the URL

RewriteCond %{REQUEST_URI} !\?
RewriteCond %{REQUEST_URI} !\&
RewriteCond %{REQUEST_URI} !\=
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !\/$
RewriteRule ^(.*[^\/])$ /$1/ [R=301,L]

An example of redirection:

URL Redirect
http://mysite/page http://mysite/page/
http://mysite/page.html http://mysite/page.html
http://mysite/page?value=1 http://mysite/page/?value=1
http://mysite/page/ without redirect
http://mysite/page/?value=1 without redirect

301 redirects from all pages of one domain to the main page of another domain

For example you need to do a 301 redirect from any URL of the current site to domain http://mysite.com

RewriteCond %{REQUEST_URI} (.*)
RewriteRule ^(.*)$ http://mysite.com/ [L,R=301]

301 redirects from each page of one domain to the same URL address of another domain

For example, consider a situation when your site has moved to a new domain, but you have a natural desire to keep users passing through the old links and open the site to a new domain with the necessary information

RewriteCond %{REQUEST_URI} (.*)
RewriteRule ^(.*)$ http://mysite.com/$1 [L,R=301]

301 redirect from the http protocol to the https protocol

To redirect from http to https protocol add the next code to .htaccess file:

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

301 redirect with protocol https to http

And the opposite example, if you do not have https protocol:

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

Delete multiple consecutive slashes in the URL and do a 301 redirect

If by chance you have any of this type of URL: mysite.com/page///my-page, then can be done 301 redirects without duplicating slashes:

RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]

Remove several hyphens (dashes) in a row and do a 301 redirect

As in the situation with repeated slashes in the URL may appear a few dashes or hyphens. For the their removal add next code:

RewriteCond %{REQUEST_URI} ^(.*)--(.*)$
RewriteRule . %1-%2 [R=301,L]

Cut index.php from URL

Example without GET parameters. For example with mysite.com/index.php to mysite.com/

RewriteCond %{REQUEST_URI} /index.php
RewriteCond %{QUERY_STRING} ^\z
RewriteRule ^(.*)$ http://mysite.ru/? [R=301,L]

Example with GET parameters. For example with mysite.com/index.php?value=1&p=3 to mysite.com/?value=1&p=3

RewriteCond %{REQUEST_URI} /index.php
RewriteRule ^(.*)$ http://mysite.ru/ [R=301,L]

A few examples of combining 2 redirects in one

To avoid consecutive redirects you can use the combined options.

301 redirect from domain with www to non-www and with slash at the end of the URL

Combining 301 redirect from the www domain on without the www and 301 redirect pages with no slash on the page with a slash in the end of the URL

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

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

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

301 redirect from domain without www to the with www and with a slash in the end of the URL

Combining 301 redirect to the domain without the www to the www with domain and 301 redirect pages with no slash on the page with a slash in the end of the URL

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

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

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

301 redirect from domain without www to the with www and without a slash at the end of the URL

Combining 301 redirect to the domain without the www to the www with domain and 301 redirect from the pages with a slash to the page without a slash at the end of the URL

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

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

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

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

301 redirect from domain with www to without www and without a slash at the end of the URL

Combining 301 redirect from the www domain on without the www and 301 redirect from the pages with a slash to the page without a slash at the end of the URL

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

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

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

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