The .htaccess file is a powerful tool for web server configuration, allowing for customized rules to manage website behavior. This article explores the effective use of .htaccess directives to implement essential redirects, enhancing user experience and improving search engine optimization (SEO). We'll delve into various redirect types, including 301 permanent redirects, 302 temporary redirects, and more, providing practical examples and best practices.
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]
Redirect 301 /old-page.htm /new-page.html
Redirect 302 /old-page.htm /new-page.html
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.domen.com/$1 [R=301,L]
Redirect 301 / https://new-domain.com/