Web Hosting

Please find below our list of Web Hosting guides to help with your hosting account.

Redirection not working, or unexpected redirection occurring

This article was posted in: Web Hosting

Redirection, when setup using the cPanel redirection tools, placed apache Mod Rewrite instructions in a special file called .htaccess (including the leading full stop).

This file will exist in the virtual root directory of the domain you are attempting to redirect.


When redirection doesn't work as expected or at all

Example - You want to redirect your cPanel's primary domain http://krystaldemo.co.uk to http://somethingelse.co.uk

In this case, the redirection instructions will be added to public_html/.htaccess

Even if you wanted to redirect http://krystaldemo.co.uk/shop/cart to http://myshop.com the redirection would still be added to public_html/.htaccess

The probable cause of redirection failing

If your website is based on a Content Management System (WordPress, Joomla, Drupal etc) or an e-commerce package then there is a good chance that this software has already stuffed .htaccess full of other instructions.

It's unreasonable to expect cPanel to understand the ins and outs of every software package in existence, so cPanel simply adds it's redirection instructions to the end of the .htaccess file (i.e. at the bottom of it.)

Here is a typical .htaccess file after WordPress has been installed:

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress
RewriteCond %{HTTP_HOST} ^krystaldemo\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.krystaldemo\.co\.uk$
RewriteRule ^shop\/$ "http\:\/\/myshop\.com\/" [R=301,L]

You can see here, that WordPress has pushed it's instructions in at the top of the .htaccess file. cPanel's instructions (the last three lines) get pushed to the bottom. So what? you might well say.

The problem is those [L] tags you see on the end of some of the WordPress instructions. This tells Apache not to process any further instructions in the .htaccess file. Therefore your redirection never gets executed.

The way to fix this is to edit this file so that it looks like this:

RewriteCond %{HTTP_HOST} ^krystaldemo\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.krystaldemo\.co\.uk$
RewriteRule ^shop\/$ "http\:\/\/myshop\.com\/" [R=301,L]
# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

Generally speaking, it is important to get any major redirection out of the way before anything else.


When redirection is unexpected and unwanted!

Redirection can be like the Spanish Inquisition - unexpected! The most common cause for unexpected redirection and infinite redirection loops is that .htaccess files are inherited by sub directories.

Lets suppose you added an Addon domain to your cPanel account called mysecondwebsite.co.uk - so your public_html directory looked a bit like this:

public_html
├── .htaccess
├── {a whole bunch of other files and directories that make up your primary website)
└── mysecondwebsite.co.uk
    ├── images
    │   └── header.jpg
    ├── index.php
    └── styles.css

The .htaccess file at the top will influence not only your primary website, but also all of your Addon website including the header.jpg file.

If one isn't careful when using mod_rewrite instructions (RewriteRule, RewriteCond etc.), then it is possible to accidentally redirect mysecondwebsite.co.uk back to the primary domain and cause all kinds of headaches and confusion.

You can prevent the public_html/.htaccess file from influencing mysecondwebsite.co.uk, above, by placing another .htaccess file at

/public_html/mysecondwebsite.co.uk/.htaccess

with this at the top of it

RewriteEngine off

This will effectively disable mod_rewrite for the Addon domain. Of course, if you still need to use mod_rewrite in your second domain, then you will need to determine exactly what it is in the public_html/.htaccess file that is upsetting your Addon domain and find a way to exclude it or move your Addon domain to a directory above public_html so that it is not influenced by it. This technique is discussed in the article Understanding Primary, Parked and AddOn Domains