Redirect a domain to subdomain www by default

Last change on 2022-11-24 • Created on 2020-01-20 • ID: KO-62E00

You can set up a default redirect without the www. prefix to a domain with the www. prefix using the mod_rewrite command.

Set up an .htaccess file with the following content in the root directory (Document Root) of the domain. Then replace "your-domain.de" with your domain name. (Do not remove any backslashes in the code!)

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.your-domain\.de [NC]
RewriteRule (.*) http://www.your-domain.de/$1 [L]

This will redirect all requests, regardless of the domain and with or without www., to the default URL http://www.your-domain.de.

If you want to point more domains to this folder, simply attach further "Conditions" directly to the code above:

RewriteCond %{HTTP_HOST} !^www\.another-domain\.de [NC]
RewriteRule (.*) http://www.another-domain.de/$1 [L]

Redirect addon domains to the main domain

Similarly, you can also redirect addon domains to the main domain. This would, for example, let you always display the same URL in the browser's address bar. Example:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?addon-domain\.de [NC]
RewriteRule (.*) http://www.main-domain.de/$1 [L]

The reverse is also possible. You can also redirect a main domain to an addon domain.

Table of Contents