A redirect sends traffic for either a domain or a specific URL to another domain or URL. 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.

Permanent vs temporary redirects

Before we give some examples of 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 it 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 will 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 the search results.

It is also useful to know that cPanel creates redirects by writing rules to the main .htaccess file. This is an important configuration file and any errors in the file may result in your website displaying a blank page. cPanel’s redirect interface works pretty well but we do recommend making a backup of the .htaccess file before you create (or remove) redirects.

Redirecting a domain to another domain

In the below example we are creating a redirect 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 and we are creating the redirect for the entire domain (and not a specific URL, such as example.net/contact). We have also opted to redirect traffic from www.example.com.

If you would look at the .htaccess file then you would see a rule like this:

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

The above rules 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 we redirected example.com and www.example.com to example.net. That works, but if someone would visit, say example.com/contact then they would not be redirected.

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 we ticked the Wild Card Redirect checkbox. The new rule will not only redirect example.com and www.example.com but also any page on the website. For instance, the URL example.com/blog/archive.php?id=356 would be redirected to example.net/blog/archive.php?id=356.

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

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

Obviously, you only want to 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 the domain name of your website. By using the wildcard redirect people visiting the old website are automatically redirected to the correct page on the new site.

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 we specified the page we want to redirect in the third field (after the forward stroke). In the previous examples we left that field blank, which is why we were redirecting the entire domain. If you specify an individual page then only that page will be redirected.

The new rewrite rule in the .htaccess file shows that the redirect should only apply 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 to see the exact same website. That is useful but not ideal for search engine optimisation. Search engines will see www.example.com and example.com as separate websites with the exact same content. Many experts consider that to be bad practice, and for that reason many website owners use a redirect rule to direct traffic.

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 will only apply to the domain www.example.net (and not example.net), and the Wild Card Redirect option will redirect 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 instead 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 will only be applied to domain example.net (and not to www.example.net), and all traffic will be 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 any traffic to the HTTP version of your site is automatically 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 will automatically use 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 serve the website over 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 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 we are creating a temporary redirect that sends all traffic to http://example.net/maintenance.html, which is our “Under Maintenance” page.

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

The redirects 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 would 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 will accept any single value. That 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. We 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 in the file. 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]

Both pages will redirect to example.net/our-story, which is not what we want. The string about/history is never processed because the string about is matched first.

The redirect would work as intended if the two rewrite rules were 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 would be matched first (and redirect to our-story/history). The string doesn’t match about, and that URL would therefore also redirect 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 will redirect correctly.

The question mark

The two rewrite rules will redirect example.net/about and example.net/about/history correctly. However, the rules will not 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 issue we could add another two rules. However, there is an easier and cleaner way to match a string with and without a trailing slash: we 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]