How do domain redirects work?

There are many options available for redirecting domains or subdomains to another section of your website or to an external address. Redirects are often carried out via .htaccess, PHP script, HTML meta tags and JavaScript.

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 destination 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 communicates 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 commercial web projects especially want to avoid. Online shops offer a constantly 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 efficiently 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 redirected to the desired website domain, which is independent of the specific URL or path that users enter.

Register your domain name
Launch your business on the right domain
  • Free website builder with .co.uk
  • Free website protection with one Wildcard SSL
  • Free 2 GB email account

What types of domain redirects are there?

A distinction is generally made between client-side and server-side redirects. With server-side domain redirects, corresponding HTTP status codes are transferred 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 inconvenience can sometimes lead to situations 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 transparent or invisible redirection. 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 forwarding can cause difficulties with indexing and the way the URL is displayed in the browser, it is not recommended unless certain limitations 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 configuration file .htaccess or a PHP script. These methods make it possible to individually 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 Permanently: The requested resource is now permanently available under the redirected URL. The old URL will remain invalid from this point on. A 301 redirect is needed here.
  • 302 – Moved Temporarily: The requested resource is available under the redirected URL. Unlike the 301 code, the original URL still remains valid.

If the HTTP status code isn’t explicitly 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 redirection destination in the search engine index.

.htaccess redirect

.htaccess is a configuration file for Apache webservers. It is used for overwriting central configurations at the directory level. This file allows website operators to carry out directory-specific settings for domains and their subdirectories. One function of the .htaccess file is server-side domain redirects of individual 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 directories, requests for the original domain are redirected server-side to the domain www.example.com:

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

This line of code begins with redirect 301 and determines which HTTP status code the server should issue. This is then followed by the path to the content that should be redirected. In the above example, all of the content is redirected. The final step is done when the complete destination URL is redirected to the user agent’s URL: http://www.example.com.

This method allows individual files to be redirected. 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 permanently redirected (/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 destination address. Caret and dollar symbols mark the beginning and the end of the path, and L labels the last mod rewrite rule for the corresponding 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 configuring a domain redirect via .htaccess, it’s important to keep in mind that incorrect entries will have serious consequences for a website’s operation. Given that these changes go into effect directly after you save the .htaccess file, configurations should always be thoroughly tested.

PHP redirects

A domain redirect can not only be done via .htaccess configuration, 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 destination URL, www.example.com:

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

When forwarding 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 destination address is also registered 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 subsequent 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 transferring HTML content to the redirect page.

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 JavaScript are available for this. The disadvantage of client-side redirects is that servers don’t deliver HTTP status codes to requesting browsers or web crawlers, meaning they are not explicitly 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 redirected.

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 destination 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.

Forwarding via HTML meta refresh

HTML meta redirects are implemented 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 corresponding notice should be set up in the HTML document. Typical examples include lines like ’Please wait a moment. You will be redirected…’. 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 destination domain, www.example.com after ten seconds.

JavaScript redirects

JavaScript offers an easy possibility for client-side domain redirects. However, as is the case with the meta tag refresh, JavaScript 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. JavaScript can also create problems for web crawlers and users with active NoScript addons. Here’s how the code looks for a domain redirect via JavaScript:

<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 destination domain located within the brackets: www.example.com.

Was this article helpful?
Page top