Domain redirects via .htaccess, PHP, HTML, and JavaScript

Domain redirects play a crucial role for large-scale web projects. The option of forwarding traffic from one website to another helps website operators better manage visitor flows and carry out site restructuring efforts. What’s more, redirects allow users to post identical content under different domains without the drawback of having these classified as duplicate content. Domain redirects furthermore prove to be efficient index management tools for search engine optimization. Regarding this task, there are many options available to website operators for redirecting domains or subdomains; these can be moved within a single web presence or forwarded to an external address. Redirects are often carried out via popular options like .htaccess, PHP script, HTML meta tags, and JavaScript.

The website builder from IONOS

MyWebsite is the turnkey solution for your professional web presence, including a personal consultant! 

SSL certificate
24/7 support

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.

Register a domain name

Build your brand on a great domain, including SSL and a personal consultant!

Private registration
24/7 support

Server-side redirects

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 redirect

.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 ‘’:

.htaccess redirect onto a new domain
redirect 301 /

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: ‘’.

This method allows individual files to be redirected. The following code shows a redirect from one website to another:

.htaccess redirect from a subdirectory to another URL
Redirect 301 /

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 (‘’) 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$ [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.

PHP redirects

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, ‘’

Redirect via PHP
header("Status: 301 Moved Permanently");

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, ‘’. 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

If carrying out a server-side domain redirect isn’t possible due to technical reasons, then website operators have the option of using client-side solutions. The HTML meta tag ‘refresh’ as well as a corresponding 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-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:

Redirect via HTML meta refresh
<meta http-equiv="refresh" content="10; url=">

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, ‘’, 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:

Redirect via JavaScript

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. ‘’.

We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.