There are many options available for re­dir­ect­ing domains or sub­do­mains to another section of your website or to an external address. Redirects are often carried out via .htaccess, PHP script, HTML meta tags and JavaS­cript.

What are domain redirects used for?

Redirects are used for informing servers that site content has been moved from one URL to another one. Doing this is necessary when the original web address is the des­tin­a­tion of an incoming link, has been saved on a user’s computer, or could occupy a prominent position in the search engine result pages (SERPs). In this case, the redirect com­mu­nic­ates to the browser or web crawler that the content has been moved, providing the user with a link to the new address instead. Without redirects, website visitors would encounter a 404 error page in place of the site they’re searching for.

This is something that com­mer­cial web projects es­pe­cially want to avoid. Online shops offer a con­stantly changing range of products that are displayed on a variety of different web pages. Once an item is no longer available, potential customers are forwarded to a page featuring similar products. This allows you to ef­fi­ciently direct visitor flows and minimise bounce rates.

When you set up a domain redirect, you can also make the same content available at different web addresses. All alternate addresses are simply re­dir­ec­ted to the desired website domain, which is in­de­pend­ent of the specific URL or path that users enter.

Register your domain name
Launch your business on the right domain
  • Free WordPress with .co.uk
  • Free website pro­tec­tion with one Wildcard SSL
  • Free Domain Connect for easy DNS setup

What types of domain redirects are there?

A dis­tinc­tion is generally made between client-side and server-side redirects. With server-side domain redirects, cor­res­pond­ing HTTP status codes are trans­ferred to user agents (browsers or web crawlers). Things look a bit different when it comes to client-side redirects. These are carried out without any sort of response, meaning no status codes are issued. This is why the latter isn’t supported by all user agents. This in­con­veni­ence can sometimes lead to situ­ations where visitors remain on the original page and aren’t forwarded anywhere. Drawbacks like these are the reason that the server-side option is often preferred for redirects. Client-side solutions should only be used when server-side domain redirects have been ruled out for technical reasons.

Another domain redirect type is the trans­par­ent or invisible re­dir­ec­tion. With this type of redirect, the URL doesn’t change. When it comes to domain redirects with masking, the URL displayed to the user is not changed, although the client has been forwarded to another domain and is being shown the content of another webpage. However, since this type of for­ward­ing can cause dif­fi­culties with indexing and the way the URL is displayed in the browser, it is not re­com­men­ded unless certain lim­it­a­tions are in place.

Tip

Want to set up a domain redirect? In the following IONOS Help Centre article, you can learn step by step how to redirect your domain to another domain.

Server-side redirects

In most cases, server-side domain redirects are carried out via the con­fig­ur­a­tion file .htaccess or a PHP script. These methods make it possible to in­di­vidu­ally define which HTTP status code should be displayed to the user agent. This allows website operators to mark domain redirects as either permanent or temporary. The HTTP status codes 301 and 302 are used for this.

  • 301 – Moved Per­man­ently: The requested resource is now per­man­ently available under the re­dir­ec­ted URL. The old URL will remain invalid from this point on. A 301 redirect is needed here.
  • 302 – Moved Tem­por­ar­ily: The requested resource is available under the re­dir­ec­ted URL. Unlike the 301 code, the original URL still remains valid.

If the HTTP status code isn’t ex­pli­citly defined, the web server issues the 302 status code during a server-side redirect. This isn’t always necessary, so it’s best to manually enter the desired status code for each redirect, as this helps reduce the chances of indexing errors, like URL hijacking, from occurring. Unlike the 301 redirect, the 302 status code informs web crawlers that the original URL should remain indexed. If this is intended to be permanent, the redirect address competes with the re­dir­ec­tion des­tin­a­tion in the search engine index.

.htaccess redirect

.htaccess is a con­fig­ur­a­tion file for Apache web­serv­ers. It is used for over­writ­ing central con­fig­ur­a­tions at the directory level. This file allows website operators to carry out directory-specific settings for domains and their sub­dir­ect­or­ies. One function of the .htaccess file is server-side domain redirects of in­di­vidu­al addresses to other URLs.

You can set up a domain redirect with the help of code in the .htaccess file. Once a .htaccess file with the following code is put into the main dir­ect­or­ies, requests for the original domain are re­dir­ec­ted server-side to the domain www.example.com:

Redirect 301 / http://www.example.com/
apacheconf

This line of code begins with redirect 301 and de­term­ines which HTTP status code the server should issue. This is then followed by the path to the content that should be re­dir­ec­ted. In the above example, all of the content is re­dir­ec­ted. The final step is done when the complete des­tin­a­tion URL is re­dir­ec­ted to the user agent’s URL: http://www.example.com.

This method allows in­di­vidu­al files to be re­dir­ec­ted. The following code shows a .htaccess redirect from one website to another:

Redirect 301 /directory/example-document.html http://www.example.com/example.html
apacheconf

After the HTTP status code 301 is taken care of, the file’s directory path, which is to be per­man­ently re­dir­ec­ted (/directory/example-document.html), and the redirect URL (http://www.example.com/example.html) are named.

Here’s how the permanent redirect would look on an Apache server with an active mod_rewrite module:

RewriteEngine On
RewriteRule ^directory/example-document.html$ http://www.example.com/example.html [L,R=301]
apacheconf

In code line 01, the Apache webserver’s mod_rewrite module is activated with the command RewriteEngine On. Following this, there’s a RewriteRule with the path of the redirect file and des­tin­a­tion address. Caret and dollar symbols mark the beginning and the end of the path, and L labels the last mod rewrite rule for the cor­res­pond­ing request. R=301 forwards the HTTP status 301.

Tip

In another article, we have compiled some .htaccess tricks for you.

You can also learn more about rewrite engines in our Digital Guide.

When con­fig­ur­ing a domain redirect via .htaccess, it’s important to keep in mind that incorrect entries will have serious con­sequences for a website’s operation. Given that these changes go into effect directly after you save the .htaccess file, con­fig­ur­a­tions should always be thor­oughly tested.

PHP redirects

A domain redirect can not only be done via .htaccess con­fig­ur­a­tion, but also carried out through a command in a PHP script (e.g., in the index.php). The following code shows a permanent redirect to the fictional des­tin­a­tion URL, www.example.com:

<?php
header("Status: 301 Moved Permanently");
header("Location: http://www.example.com");
exit;
?>
php

When for­ward­ing via PHP script, the intended HTTP status code is defined through the header function in the second line of code. In this example, a permanent 301 redirect is to be carried out. Given that server-side redirects are normally executed on a temporary basis, active steps need to be taken to carry out permanent redirects via the 301 status code. With permanent redirects, the domain redirect’s des­tin­a­tion address is also re­gistered with header. The redirect in this example then goes to the address, http://www.example.com. The exit function in line 04 of the code ends the script and prevents a sub­sequent line of code from being executed. In order for redirects to work via PHP script, the code block has to be at the beginning of the HTML page. This prevents the server from trans­fer­ring HTML content to the redirect page.

Domain Checker

Client-side redirects

If carrying out a server-side domain redirect isn’t possible due to technical reasons, website operators have the option of using client-side solutions. The HTML meta tag refresh as well as JavaS­cript are available for this. The dis­ad­vant­age of client-side redirects is that servers don’t deliver HTTP status codes to re­quest­ing browsers or web crawlers, meaning they are not ex­pli­citly informed of the redirect. What’s more, client-side redirects aren’t supported by all user agents, which means there’s a risk that not all website visitors will be re­dir­ec­ted.

Client-side redirects have a negative effect on search engine indexing. Explicit exclusion from indexing through the HTTP status code 301 doesn’t happen with client-side redirects. This can lead to redirect domains competing with des­tin­a­tion domains when it comes to search engine ranking. Unlike server-side redirects, which largely remain invisible to users, client-side redirects are always coupled with delays, which some users may end up noticing.

For­ward­ing via HTML meta refresh

HTML meta redirects are im­ple­men­ted through meta tags with the attribute http-equiv. All that’s needed for this is a simple HTML file and a matching tag in the header for creating redirects. In order for visitors to your website to be informed of the redirect, a cor­res­pond­ing notice should be set up in the HTML document. Typical examples include lines like ’Please wait a moment. You will be re­dir­ec­ted…’. A simple domain redirect via HTML meta refresh looks like this:

<meta http-equiv="refresh" content="10; url=http://www.example.com/">
html

The client is prompted to redirect the domain through the meta tag http-equiv="refresh". How this happens can be defined in the content attribute. The example redirects users to the des­tin­a­tion domain, www.example.com after ten seconds.

JavaS­cript redirects

JavaS­cript offers an easy pos­sib­il­ity for client-side domain redirects. However, as is the case with the meta tag refresh, JavaS­cript redirects should only be used on a case-by-case basis, as this script-based language is not supported by every web browser due to security concerns. JavaS­cript can also create problems for web crawlers and users with active NoScript addons. Here’s how the code looks for a domain redirect via JavaS­cript:

<script> 
window.location.replace('http://www.example.com'); 
</script>
html

The most important thing here is the third line of code. In the example code, the object, window.location is used in order to reference the current website address. The command replace instructs the browser to direct the user to the des­tin­a­tion domain located within the brackets: www.example.com.

Go to Main Menu