There are several reasons, why you need to redirect a domain to another one. One may be that you have changed the domain for your website. In this case you would not want to break the existing links of your website for serveral reasons. One of them might be the indexation of your site by search engines such as google. Luckily there is an easy SEO friendly solution using the .htaccess file. Instead of just providing the code to you, I will make a complete breakdown, so you will be able to understand it properly.
What is .htaccess and mod_rewrite
First we need to clarify which technologies we use to implement such a redirection. These would be the .htaccess file and the mod_rewrite module.
.htaccess
The .htaccess file is a server configuration file, often found on apache servers. We can use it to override configurations of certain apache modules, for the folders the .htaccess file is stored in. CMS applications such as WordPress for example have a .htaccess file in their root directories to redirect all requests to the index.php file, which will initiate the application. The file effects all subfolders of its directory, but can also be overwritten by other .htaccess files on deeper hierarchy levels.
mod_rewrite
mod_rewrite is a module found on apache servers, used to access request URLs and manipulate them before the application can process the request and the response is sent to the client.
Implementing the 301 redirect
If you don’t have a .htaccess file yet, create it in the root directory of your application or website. We start the mod_rewrite module using RewriteEngine On. Using the RewriteBase command we can determine for which requested paths our redirection rule should apply. By adding a slash (/), we tell the rules to be applied to all routes deeper than the directory the file is stored in. Since we placed it within the root directory, the rule will apply to all requests.
RewriteEngine On
RewriteBase /
Note: If you are using WordPress or any other application you might already find a .htaccess file containing such rewrite rules. In this case the lines above most certainly already exist. In this case make sure, to skip the part above and add the code showed within the next parts directly after those lines.
Rewrite Condition
Now that the mod is started we need to write a conditon to which our rule should apply. Since we only want one primary domain for our websites, the best practice would be to redirect all request which do not call said domain. In other words our condition should only apply if any other than the primary domain is called.
To initiate a new condition we use the RewriteCond command. This command consists of tow parameters and accepts some flags:
RewriteCond param1 param2 [flags]
After that, we need to declare which value of the request we want to check. mod_rewrite allows us to access certain parts of the request using predefined variables. Those variables are accessed via the syntax %{variablename}. In our case we need the domain requested by the client, which is stored within the «HTTP_HOST» variable.
RewriteCond %{HTTP_HOST} param2 [flags]
Note: The «HTTP_HOST» variable contains only the domain name. URI parts such as the protocol (http / https), paths and query parameters are not part of it.
Next we need to define the conditional value to check, which in our case is the new domain name. Since we want the rule only to apply if the given domain is unequal to our primary one, we can make use of the «!» (unequal) comparator in front of our domain. We end our condition by adding a dollar sign ($) at the end of our domain. This character is used to mark the end of a string.
RewriteCond %{HTTP_HOST} !www.your-domain.com$ [flags]
The last bit of code we need to add to this line is a flag. mod_rewrite has certain flags that can be used for commands to be executed a certain way. In our case we add the «NC» (for nocase) flag. This makes our condition non case sensitive.
RewriteCond %{HTTP_HOST} !www.your-domain.com$ [NC]
Note: The «NC» flag is not really necessary but strongly recommended since the condition would not work if the domain were to be entered in uppercase and the [NC] flag wasn’t set.
Rewrite Rule
We finish our command by adding the rewrite rule itself, which needs to perform a HTTP redirect using the HTTP status code 301. The 301 status code implies that the requested ressource (request url) is permanently moved to another location (new domain / www.our-domain.com). This status code is SEO friendly, means search engines will recognize it and index the website properly, without the domains canibalising each other. Like the RewriteCond, the RewriteRule consists of two parameters and accepts flags.
RewriteRule param1 param2 [flags]
The first parameter would be the request path which needs to be overwritten. This parameter allows us to use regular expressions. Using the expression (.*) tells the rule to redirect any / all paths, containing all its query parameters. Together, with our previously defined RewriteCond, this rule would now apply to any request which does not include our main domain (www.your-domain.com), no matter what path is given.
RewriteRule (.*) param2 [flags]
The second parameter must contain the URI we like to redirect to. Our task here is to somehow pass the requested path to the new URI. Using the dollar Sign ($) and a specific number, we are able to access the values evaluated by the regular expression we defined in the first parameter. The number defines the level of the regular expression we want to use. Since we only have one level, «$1» will do the trick. So we can simply add this to the end of our new URI. Now our rule preserves the given path and all its query parameters.
RewriteRule (.*) https://www.your-domain.com/$1 [flags]
Like we did before, we now add some required flags. As mentioned, the redirect should use the HTTP status code 301. By default the RewriteRule command will only overwrite the URI displayed within the browser and not redirect requests. Using a flag called «R» (for «redirect») we can force mod_rewrite to perform a redirect. The «R» flag accepts a HTTP status code as input, which in our case would be 301.
If you use a CMS like WordPress, the .htaccess file will use many other RewriteRules. To not manipulate any of them, our rule needs to be wrapped up, by marking the end of it. The flag «L» (for «last») will do exatcly that and allows for more rewrite rules to be made. Remember to separate multiple flags using a comma «,».
RewriteRule (.*) https://www.your-domain.com/$1 [R=301,L]
Full Code
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.your-domain.com$ [NC]
RewriteRule (.*) https://www.your-domain.com/$1 [R=301,L]
Useful Links
https://developer.mozilla.org/de/docs/Web/HTTP/Status
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html