Set up HTTP 301 redirects via .htaccess

In this help center article you will learn how to set up HTTP 301 redirects for your server using the Apache2 mod_rewrite extension and the .htaccess file.


General setup

A .htaccess file is required in your document root (root directory) to set up mod_rewrite rules. If no file has been created yet, please create an .htaccess file with the following content:


RewriteEngine On


Tip: Structure your mod_rewrite rules to avoid multiple 301 redirects. Example: Instead of http://domain.dehttps://domain.dehttps://ww.domain.de, you should immediately redirect from http://domain.de to https://ww.domain.de.



Forwarding of individual URLs

To redirect specific URLs, please use the following scheme:


Redirect 301 /old-product-detail-page/ https://www.domain.de/neue-podukt-detailseite/


Example: www.domain.de/alte-produkt-detailseite/ 301 → www.domain.de/neue-produkt-detailseite/


Forwarding of individual directories

To forward entire directories including the existing subdirectories, please use the following rule:


RewriteRule ^old-product-category/(.*)$ /new-product-category/$1 [R=301,NC,L]



Forwarding to www. subdomain

Please use the following rule to redirect all pages that are not accessed via the www. subdomain:


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


Example: https://domain.de/produkt-detailseite 301https://www.domain.de/produkt-detailseite



Forwarding to https://

Please use the following rules for redirecting all URLs to https://:


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

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


Examples:

source URL destination URL
http://domain.de https://domain.de
http://www.domain.de https://www.domain.de
http://domain.de/produkt-detailseite https://domain.de/produkt-detailseite
http://www.domain.de/produkt-detailseite https://www.domain.de/produkt-detailseite



Forwarding to new domain

To redirect an entire domain, use the following rule if the request URI remains identical:

RewriteCond %{HTTP_HOST} !^www\.alte-domain\.de$ [NC]
RewriteRule .* https://www.neue-domain.de%{REQUEST_URI} [L,R=301]



Troubleshooting

After setting up new mod_rewrite rules, they should be checked for all possible constellations. The external debugging tool https://help.creoline.com/de/tools/redirect-test is suitable for this. Enter all variants of the URL here to check the various redirects.


Attention: Current browsers use an internal cache for 301 redirects, which is not automatically emptied by emptying the browser cache.