Useful redirects with .htaccess

.htaccess Useful redirects with .htaccess

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.

301 redirect from HTTP to HTTPS

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

301 redirect from WWW to non-WWW

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

Simple 301 Redirect

Redirect 301 /old-page.htm /new-page.html

Simple 302 Redirect

Redirect 302 /old-page.htm /new-page.html

301 redirect from non-WWW to WWW

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

Redirect to new domain name

Redirect 301 / https://new-domain.com/