2.3.4 301 redirect using .htaccess
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:
More detailed:
2. 301 Redirect from www to non-www (primarry mirror - domain without www):
At the beginning of the .htaccess file, add:
3. 301 Redirect from non-www to www (primary mirror - domain with www):
4. 301 Redirect from slashed to non-slashed Pages (entire Website):
5. 301 Redirect from non-slashed to slashed pages (often set automatically in CMS):
6. Single (not two sequential) 301 Redirect for non-www and slashed end of page address:
7. Single (not two sequential) 301 Redirect for www and slashed end of page address:
8. Single (not two sequential) 301 Redirect for www and non-slashed end of page address:
9. Single (not two Sequential) 301 Redirect for non-www and non-Slashed end of page address:
10. 301 Redirect only for example.com/index.php
address (Without GET Parameters) to the main mirror example.com
:
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
:
12. 301 Redirect for URLs with GET Parameters (dynamic URLs) to static URLs:
Option #1 (Simple address with GET Parameter):
Option #2 (From a page with a GET Parameter):