Introduction: The .htaccess file is a powerful configuration file used on Apache web servers to control various aspects of website behavior, including URL redirection. Redirect rules defined in the .htaccess file allow webmasters to efficiently manage URL redirects, domain changes, and other redirection scenarios. In this blog post, we’ll explore a comprehensive list of .htaccess redirect rules and explain what each rule does to help you effectively manage website redirections.
- Redirecting a Single Page: Redirect 301 /old-page.html /new-page.html
- Redirects traffic from a specific old page to a new page using a permanent (301) redirect.
- Redirecting an Entire Domain: Redirect 301 / https://www.newdomain.com/
- Redirects all traffic from an old domain to a new domain using a permanent (301) redirect.
- Redirecting Non-WWW to WWW: RewriteEngine On RewriteCond %{HTTP_HOST} !^www. [NC] RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
- Redirects traffic from a non-www version of the domain to the www version using a permanent (301) redirect.
- Redirecting WWW to Non-WWW: RewriteEngine On RewriteCond %{HTTP_HOST} ^www.(.)$ [NC] RewriteRule ^(.)$ http://%1/$1 [R=301,L]
- Redirects traffic from the www version of the domain to the non-www version using a permanent (301) redirect.
- Redirecting HTTP to HTTPS: RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
- Redirects traffic from HTTP to HTTPS using a permanent (301) redirect.
- Redirecting a Directory: RedirectMatch 301 ^/old-directory/(.*)$ /new-directory/$1
- Redirects all traffic from an old directory to a new directory using a permanent (301) redirect.
- Redirecting All Pages to a Single Page: RedirectMatch 301 ^/.*$ /single-page.html
- Redirects all traffic from any page on the website to a single-page using a permanent (301) redirect.
- Redirecting with Query String Parameters: RewriteCond %{QUERY_STRING} ^id=123$ RewriteRule ^index.php$ /new-page? [R=301,L]
- Redirects traffic from a specific page with query string parameters to a new page without parameters using a permanent (301) redirect.
Conclusion: The .htaccess file is a powerful tool for managing URL redirections and other server configurations on Apache web servers. By understanding and implementing the various .htaccess redirect rules discussed in this guide, webmasters can efficiently manage URL redirects, domain changes, and other redirection scenarios to improve website usability, SEO, and user experience. However, it’s essential to use redirect rules responsibly and test them thoroughly to avoid unintended consequences or errors on the website.