301 redirects for your Drupal website (.htaccess file)

Very often you will need to apply a 301 redirect to your Drupal website. You can do so by adding the following codes to your .htaccess file in the Drupal root directory. If you don't redirect either "non-www to www" or "www to non-www" search engines such as Google consider your www and non-www website as "duplicated content", what might be penalised. Another important case is the redirection of an old domain to a new domain. The code below, redirects not only the root domain, but all paths/files, what is absolutely key in most cases.

Generic redirect non-www to www in .htaccess

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

Generic redirect non-www to www https in .htaccess

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

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Generic redirect www to non-www in .htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Generic redirect www to non-www https in .htaccess

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

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Redirect an old domain to a new domain in .htaccess

Works for http and https, but SSL certificate has to be active, otherwise first error "site not secure"

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.net/$1 [L,R=301,NC]

Redirect an old domain to one particular page in .htaccess

RewriteCond %{HTTP_HOST} ^(www\.)?name-of-domain\.net [NC]
RewriteRule ^(.*)$ http://www.name-der-domain.de/unterseite.html [R=301,L]

https://jweiland.net/know-how/internet/htaccess-konfigurieren.html#c2756

Taxonomy: 

Drupal themes to be compared