2.3.4 301 redirect using .htaccess

301 редирект баннер

Usage of .htaccess

Redirects play a vital role in web development, allowing you to guide visitors from one page to another. One of the most common types of redirects is 301 Moved Permanently, which informs search engines and browsers that a page has been permanently moved. It’s a crucial tool for managing your site’s structure, enabling you to redirect visitors from one page to another address.

In this article, we will explore examples of ready-made 301 redirects using the .htaccess file, which can reside either in the root of your site or be created in any of its directories. Each .htaccess file applies to all subdirectories within the directory it’s located. This means that settings defined in .htaccess inside a specific directory will apply to all files and subdirectories within that directory.

Examples of .htaccess 301 Redirects

1. The simplest scenario - a 301 redirect from one page to another:
.htaccess
Redirect 301 /example-1/ http://example.com/page-2/

More detailed:

.htaccess
RewriteCond %{REQUEST_URI} ^/page/$
RewriteRule ^.*$ http://example.com/new-page/? [R=301,L]
2. 301 Redirect from www to non-www (primarry mirror - domain without www):
.htaccess
RewriteCond %{HTTP_HOST} ^www.(.)$
RewriteRule ^(.)$ http://%1/$1 [L,R=301]

At the beginning of the .htaccess file, add:

.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.(.)$ [NC]
RewriteRule ^(.)$ http://%1/$1 [R=301,L]
3. 301 Redirect from non-www to www (primary mirror - domain with www):
.htaccess
RewriteCond %{HTTP_HOST} ^([^www].)$
RewriteRule ^(.)$ http://www.%1/$1 [L,R=301]
4. 301 Redirect from slashed to non-slashed Pages (entire Website):
.htaccess
RewriteCond %{REQUEST_URI} !?
RewriteCond %{REQUEST_URI} !&
RewriteCond %{REQUEST_URI} !=
RewriteCond %{REQUEST_URI} !.
RewriteCond %{REQUEST_URI} ![^/]$
RewriteRule ^(.*)/$ /$1 [R=301,L]
5. 301 Redirect from non-slashed to slashed pages (often set automatically in CMS):
.htaccess
RewriteCond %{REQUEST_URI} !?
RewriteCond %{REQUEST_URI} !&
RewriteCond %{REQUEST_URI} !=
RewriteCond %{REQUEST_URI} !.
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]
6. Single (not two sequential) 301 Redirect for non-www and slashed end of page address:
.htaccess
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]
.htaccess
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]
.htaccess
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]
7. Single (not two sequential) 301 Redirect for www and slashed end of page address:
.htaccess
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]
.htaccess
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]
.htaccess
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]
8. Single (not two sequential) 301 Redirect for www and non-slashed end of page address:
.htaccess
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP_HOST} ^([^www].)$
RewriteRule ^(.)$ http://www.%1/$1 [L,R=301]
.htaccess
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]
.htaccess
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]
.htaccess
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]
9. Single (not two Sequential) 301 Redirect for non-www and non-Slashed end of page address:
.htaccess
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP_HOST} ^www.(.)$
RewriteRule ^(.)$ http://%1/$1 [L,R=301]
.htaccess
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]
.htaccess
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]
.htaccess
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]
10. 301 Redirect only for example.com/index.php address (Without GET Parameters) to the main mirror example.com:
.htaccess
RewriteCond %{REQUEST_URI} /index.php
RewriteCond %{QUERY_STRING} ^\z
RewriteRule ^(.*)$ http://example.com/? [R=301,L]
11. 301 Redirect for all addresses with index.php and GET Parameters to pages with only GET Parameters (cut out index.php in the URL):

Example - redirecting example.com/index.php?n=1 to example.com/?n=1:

.htaccess
RewriteCond %{REQUEST_URI} /index.php
RewriteRule ^(.*)$ http://example.com/ [R=301,L]
12. 301 Redirect for URLs with GET Parameters (dynamic URLs) to static URLs:

Option #1 (Simple address with GET Parameter):

.htaccess
RewriteCond %{QUERY_STRING} ^id=229
RewriteRule ^.*$ /supermodel/? [R=301,L]

Option #2 (From a page with a GET Parameter):

.htaccess
RewriteCond %{REQUEST_URI} /test/
RewriteCond %{QUERY_STRING} ^id=229
RewriteRule ^.*$ /supermodel/? [R=301,L]
13. All pages of one domain to the main page of another domain:
.htaccess
RewriteCond %{REQUEST_URI} (.)
RewriteRule ^(.)$ http://example.com/ [L,R=301]
14. Each page of one domain to the same address of another URL:
.htaccess
RewriteCond %{REQUEST_URI} (.)
RewriteRule ^(.)$ http://example.com/$1 [L,R=301]