.htaccess tricks – 301 redirect, all www

Tutorial – Tips and tricks for .htaccess

Redirect to non www and https

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www. [NC]
RewriteCond %{HTTP_HOST} ^(?:www.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

Redirect to https and www

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$  [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]

Add ErrorDocument

ErrorDocument 404 https://example.com/error.php

301 Redirect

Redirect 301 /project/ https://exampler.com/project/
Redirect 301 /project.php https://exampler.com/project/
Redirect 301 /project/ https://exampler.com/pro-cat/project/