Last updated: 5 April 2021

A redirect sends traffic for either a domain or a specific page to another domain or page. There are various reasons why you may want to create one or more redirects. Here are some common use-cases:

  • Send traffic to a different domain. For example, you can set up a redirect from example.com to example.net.
  • Force all traffic to use the ‘www’ subdomain (i.e. redirect example.net to www.example.net). The opposite is also possible.
  • Avoid “page not found” errors by redirecting pages whose URL has changed.
  • Display a maintenance page when you are working on your site.

You can create and manage redirects via cPanel’s Redirects interface. This article covers the most common redirect rules. I also look at how the redirect rules look in the .htaccess file. Understanding how redirect rules work in the background is mainly useful for more advanced rules that can’t be created via cPanel.


Note: The .htaccess file is an important configuration file. Any errors in the file can result in your website displaying a blank page. cPanel’s Redirects interface works pretty well but I do recommend making a backup of your .htaccess file before you create (or remove) redirects.


Permanent vs temporary redirects

It is useful to know that there are two types of redirects:

  • A permanent redirect tells search engines that the domain (or URL) has moved. The server will return a 301 status code (“Moved Permanently”) and search engines such as Google should update the URL shown in search results.
  • A temporary redirect also redirects traffic but does not ask search engines to update their database. This type of redirect uses the status code 302 (“Moved Temporarily”).

Note: The cPanel interface suggests that web browsers update bookmarks when they encounter a permanent redirect. Although browsers probably should do that, none of the major browsers does. Search engines, however, do use the status code to update their search results.


Redirecting a domain to another domain

The below image shows a rule that redirects traffic from example.com to example.net.

Setting up a direct to another domain.
Image: creating a redirect from example.com to example.net.

This is a permanent redirect that redirects the entire domain (and not a specific URL, such as example.net/contact). It also redirects traffic from www.example.com.

In your .htaccess file the rule looks like this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "http\:\/\/example\.net\/" [R=301,L]

The rule tells the web server that requests for example.com or www.example.com should be redirected to http://example.net with the status code 301.


If you got the cURL command-line utility installed on your computer then you can see exactly how traffic is redirected, as shown in the below (shortened) output:

$ curl -IL example.com
HTTP/1.1 301 Moved Permanently
Location: http://example.net/

Redirecting all pages on a domain

In the previous example I redirected both example.com and www.example.com to example.net. That works, but only for the main domain. Someone who visits a specific page on your website is not redirected. For instance, the page example.com/contact is not redirected to example.net/contact.

If you want to redirect all pages for a domain then you can tick the Wild Card Redirect checkbox.

Setting up a wildcard redirect.
Image: creating a wildcard redirect.

The only difference compared with the previous example is that I ticked the Wild Card Redirect checkbox. The new rule redirects example.com and www.example.com and any page on the website.

The new rewrite rule in the .htaccess file is slightly different: it looks for any string after the domain name (^(.*)$) and appends it to the new URL (/$1).

RewriteRule ^(.*)$ "http\:\/\/example\.net\/$1" [R=301,L]

Obviously, you should only use the wildcard redirect if all the pages on the old site exist on the new site. This option is mainly useful if you are changing your website’s domain name.

Redirecting individual pages

Redirects are also commonly used to redirect individual pages. Say, for example, that you have have created a new contact page for example.net. The old page was at example.net/contact-us but you now want people to go to example.net/contact. To do so, you only have to enter the page you want to redirect and the new location:

Setting up a redirect for an individual page.
Image: creating a redirect for a page (example.net/contact-us).

Note that I specified the page to be redirected in the third field (after the forward stroke). In the previous examples the field was blank. If you specify an individual page then only that page is redirected.

The new rewrite rule in the .htaccess file shows that the redirect only applies to the string ^contact\-us$ and that the new location is /contact:

RewriteRule ^contact\-us$ "http\:\/\/example\.net\/contact" [R=301,L]

Redirecting www to non-www and vice versa

By default, your website is accessible with and without the ‘www’ subdomain. It doesn’t matter if people visit www.example.net or example.net: they will get the exact same website. That is useful but not ideal for search engine optimisation (SEO). Search engines see www.example.com and example.com as separate websites with the exact same content. Many SEO experts consider that bad practice, and for that reason many website owners use a redirect rule to either always or never use the ‘www’ subdomain.

You can redirect traffic so that your website never uses the ‘www’ subdomain as follows:

  • Use Permanent (301) as the redirection Type
  • If you got more than one domain on your account, select the relevant domain
  • Enter the URL without the ‘www’ subdomain under Redirects to
  • Tick the Only redirect with www checkbox
  • Tick the Wild Card Redirect checkbox

Setting up a redirect to the non-www version of a domain.
Image: creating a redirect to strip the www subdomain.

The Only redirect with www option ensures that the rewrite rule only applies to the domain www.example.net (and not example.net), and the Wild Card Redirect option redirects any page on the site. The rule itself looks like this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.net$
RewriteRule ^(.*)$ "http\:\/\/example\.com\/$1" [R=301,L]

If you prefer to always use the ‘www’ subdomain then you need to tweak just two settings:

  • Under Redirects to, enter the domain including the ‘www’ subdomain
  • Tick the Do Not Redirect www checkbox

Setting up a redirect to the www version of a domain.
Image: creating a redirect to redirect all traffic to the www subdomain.

Now, the rewrite rule only applies to the domain example.net (and not to www.example.net), and all traffic is redirected to the ‘www’ version of the site:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.net$
RewriteRule ^(.*)$ "http\:\/\/www\.example\.net\/$1" [R=301,L]

Redirect to HTTPS

If your website has an SSL certificate then you probably want to make sure that all traffic to the HTTP version of your site is  redirected to the HTTPS version. You can again do that by creating a redirect rule:

  • Use Permanent (301) as the redirect Type
  • If you got more than one domain on your account, select the relevant domain
  • Enter the HTTPS-URL under Redirects to
  • Tick the Wild Card Redirect checkbox

Setting up a redirect to force traffic to use HTTPS.
Image: creating a redirect to enforce HTTPS.

With these options all traffic automatically uses SSL. In case you are curious, the redirect rule looks like this:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^example\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.net$
RewriteRule ^(.*)$ "https\:\/\/example\.net\/$1" [R=301,L]

A few rewrite conditions have been added to the rule. cPanel is smart enough to understand that you want to enforce HTTPS.


Note: There are often easier ways to enforce HTTPS. For instance, content management systems such as WordPress let you change the website URL via the WordPress dashboard (under Settings > General). Changing the base URL to the HTTPS domain is often all that is needed to always use HTTPS.


Combining WWW and HTTPS redirects

If you want to redirect your website to either the www or non-www version and enforce HTTPS then you need to create two separate rules:

  • A rule to redirect either www or non-www traffic to the HTTPS version of your site.
  • A rule to redirect all other traffic to the HTTPS version of your site.

Unfortunately, it is not possible to create the rules needed for this via cPanel’s Redirect interface. You instead need to manually add the rules to the .htaccess file (which you can do via the file manager. You can use either of the following rules.

Redirect to non-WWW and HTTPS

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

Redirect to WWW and HTTPS

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]

Using temporary redirects

Temporary redirects are created in exactly the same way as permanent redirects. The only difference is that you need to select Temporary (302) as the type of redirect, rather than Permanent (301).

Temporary redirects are often used to direct all traffic to a maintenance page while you are working on a website. In the below example I created a temporary redirect that sends all traffic to http://example.net/maintenance.html.

Setting up a temporary redirect to a maintenance page.
Image: creating a temporary redirect.

The redirect works but there is a slight problem… When you are working on a website you probably want all traffic to be redirected to the maintenance page, apart from traffic that comes from your IP address.

You can achieve this by manually editing the redirect rule. It just needs one extra rewrite condition that says: “only process these rules if the visitor’s IP address is not my IP address”. Here is an example of such a rule:

RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^12\.34\.56\.78
RewriteCond %{HTTP_HOST} ^example\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.net$
RewriteRule ^/?$ "http\:\/\/example\.net\/maintenance\.html" [R=302,L]

The IP check happens on the second line. Of course, you need to replace the IP address shown with your own IP address (you can check your IP via findip.co). Also note that the dots in the IP address need to be escaped by backward strokes.


Note: in .htaccess files the dot character is a “wildcard character” that represents any other character. The character can be a dot, so strictly speaking you don’t need to escape the dots in the IP address. However, it is best practise to always escape dots that should be taken literally (and not be a wildcard).


Wildcards in redirect rules

There are a few other special characters that frequently appear in redirect rules. I will briefly look at three more wildcards: the caret (^), dollar sign ($) and question mark (?).

The importance of anchors

Redirect rules in .htaccess files are processed in the order in which they appear. This can have unintended consequences.

For example, let’s imagine that you got an ‘About’ page at example.net/about and that the page has one sub-page: example.net/about/history. You want to change the URL to example.net/our-story and you therefore create two redirect rules:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.net$
RewriteRule about "http\:\/\/example\.net\/our-story" [R=301,L]
RewriteRule about/history "http\:\/\/example\.net\/our-story/history" [R=301,L]

The rules won’t work. The string about/history is never processed because the string about is matched first. In effect, the second rule doesn’t exist.

The redirect does work if the two rules are reversed:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.net$
RewriteRule about/history "http\:\/\/example\.net\/our-story/history" [R=301,L]
RewriteRule about "http\:\/\/example\.net\/our-story" [R=301,L]

Now, the string about/history is matched first (and redirects to our-story/history). The string doesn’t match about, and that URL is therefore also redirected correctly (to our-story).

There is a better way to prevent these sorts of redirection nightmares. You can use to caret (^) and dollar ($) symbols to mark the start and end of a string that should be matched. These redirection rules will work correctly:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.net$
RewriteRule ^about$ "http\:\/\/example\.net\/our-story" [R=301,L]
RewriteRule ^about/history$ "http\:\/\/example\.net\/our-story/history" [R=301,L]

The first rules only matches a string about. If there is anything before or after the string then there is no match. The redirect for about/history is therefore processed and redirects correctly.

The question mark

The two rewrite rules redirect example.net/about and example.net/about/history correctly. However, the rules don’t redirect the URLs if they include a trailing slash: both example.net/about/ and example.net/about/history/ would not be redirected.

To correct this you could add another two rules. However, there is an easier and cleaner way to match a string with and without a trailing slash: you can use the question mark wildcard. The question mark matches the preceding character zero or one times. So, instead of matching the strings about and about/history we can match about/? and about/history/?:

RewriteRule ^about/?$ "http\:\/\/example\.net\/our-story" [R=301,L]
RewriteRule ^about/history/?$ "http\:\/\/example\.net\/our-story/history" [R=301,L]