If your online project is growing and beginning to attract international customers, the workload of your server is also increasing. High user numbers and the large geographical distribution of the clients result in ever-increasing loading times and slow transfer rates. By using a CDN (content delivery network), you’ll be able to react to increasing demands and optimize your data delivery.
Redirecting website domains
Essentially, 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 target of an incoming link, has been saved on a visitor’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 desired content has been moved, providing the user with a link to the new address instead.
Without such redirects, website operators would encounter a 404 error page in place of the searched site. This is something that commercial web projects especially seek to avoid. Online shops offer a constantly changing range of products that’s displayed throughout a variety of different web pages. Once an item is no longer available, potential clients are then forwarded to a page featuring similar products. This allows the flow of visitors to be more efficiently guided and also minimizes bounce rates. A domain redirect furthermore enables identical content to be accessed under different web addresses. During this step, all the alternative addresses are redirected towards the website’s prioritized domain.
Types of domain redirects
There’s generally a distinction 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-mentioned variant isn’t supported by every user agent. This inconvenience can sometimes even lead to situations where visitors remain on the original website and aren’t forwarded to the new page. Such drawbacks make setting up this server-side option the preferred measure for redirects. Client-side solutions should therefore only be used when server-side domain redirects are ruled out due to technical obstructions.
In most cases, server-side domain redirects are carried out via the configuration file, .htaccess, or a PHP script. The advantage these methods offer is that it’s 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. Here, the HTTP status codes 301 and 302 are relevant.
- 301 – Moved Permanently: the requested resource is now permanently available under the redirected URL. The old URL will remain invalid from this point on.
- 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 transfers the status code 302 during a server-side redirect. This isn’t always necessary, and it’s recommended to manually enter the desired status code during each redirect, as this helps reduce the chances of indexing errors, like URL hijacking, from occurring. Unlike the 301 redirect, the status code 302 informs web crawlers that the original URL should remain indexed. When intended for permanent operation, the redirect address competes with the redirection destination in the search engine index.
Using a proper redirect ensures that inbound links are redirected accordingly, helping the website maintain its search rankings and preventing a dip traffic.
.htaccess is a configuration file for Apache webservers used for overwriting central configuration at the directory level. This file allows website operators to carry out directory-specific settings for domains and their subdirectories. One of the .htaccess’ file functions involves server-side redirects of individual addresses to other URLs.
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/
The line of code begins with ‘redirect 301’ and determines the HTTP status code that’s to be transferred by the server. This is then followed by the path to the content that’s to be redirected. As laid out by the forward slash above, all of the content would be redirected in this case. The final step is reached 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 redirect from one website to another:
Redirect 301 / http://www.example.com/
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 could 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]
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.
When configuring a domain redirect via .htaccess it’s important to pay attention to the fact that erroneous entries may have serious effects on a website’s operation. Given that these changes go into effect directly after saving the .htaccess file, the matching configurations should always be thoroughly tested.
A domain redirect can also be carried out through a command in a PHP script (e.g. in the index.php). The following code displays a permanent redirect to the mock destination URL, ‘www.example.com’
<?php header("Status: 301 Moved Permanently"); header("Location: http://www.beispiel.de"); exit; ?>
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, permanent redirects occurring via status code 301 have to be explicitly forced into action. The domain redirect’s destination address is also registered here 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.
Avoid redirect chains! Too many of those can lead to a dip in traffic.
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-related matters. 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 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 forwarded...’. A simple domain redirect via HTML meta refresh looks as follows:
<meta http-equiv="refresh" content="10; url=http://www.example.com/">
Client are prompted to domain redirects through the meta tag http-equiv=‘refresh‘. Just how this happens can be defined in the content attribute. The example above redirects users to the destination domain, ‘www.example.com’, after ten seconds.
<script> <!-- window.location.replace('http://www.example.com'); //--> </script>
The most important thing here is the third line of code. In the example code, the object, ‘window.location’ is used in order to make reference to the current website address. The command ‘replace’ instructs the browser to direct the user to the destination domain located within the brackets, i.e. ‘www.example.com’.