Redirect Configuration
Posted 04th октомври, 2018
Redirects are used to direct viewers away from a particular site, or page on a site, to another designated URL. For example:
- To send users from an old website's URL to a newly designed website
- To send users to a holding page, possibly specifying the page they’re looking for isn’t there anymore or that it is currently undergoing maintenance or construction.
- To redirect one version of a domain (e.g. .co.uk) to another (.com). This is often in conjunction with addon or parked domains.
- Because redirects also apply to search engines, redirects can send the SEO benefit of one URL to another.
Contents
- 301 vs 302 Redirection
- Redirect Cheat Sheet
- Common Issues
301 vs 302 Redirection
There are a number of different redirects, the most popular being 301 (Permanent) and 302 (Temporary).
A 301 redirect tells search engines that a redirect is permanent and that the SEO benefit from the old domain should be passed on to the redirected page. Typically between 90-99% of the SEO ranking power is passed on. A permanent 301 redirect is commonly used when a new domain name has been purchased and a user wants to redirect the traffic from the old site to the new web address.
A 302 redirect indicates to search engines that the redirection is temporary. A common use for a 302 redirect is when you have a site with a page still under construction, but you want to let the public know that it’s being worked on and will be live soon, so you could redirect to a placeholder page with information of what’s being worked on.
Redirect Cheat Sheet
Redirects are htaccess directives which are often set in a system file called .htaccess
.
Redirect an entire website
Redirect 301 / http://example.co.uk
(replace example.co.uk with the address to be redirected to)
Redirect an individual page
Redirect 301 /index.html http://example.co.uk
(replace index.html with the file for that page and example.co.uk with the address to be redirected to)
Note: You will need to add this line for each page you wish to redirect. If the page you wish to redirect isn't in the home directory, you will need to include the full address (not including the home directory) e.g. /folder/index.html
Redirect non-www to www
To force your page to always load from the www address, add the following code to your .htaccess file in your public_html:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Brief explanation on the rules:
- Options +FollowSymlinks - The server will follow symbolic links in this directory.
- [NC] (nocase) - the [NC] flag causes the RewriteRule to be matched in a case-insensitive manner.
- [L] (last) - the [L] flag indicates that the current rule should be applied immediately without considering further rules.
- [R] (redirect) - the [R] flag causes a HTTP redirect to be issued to the browser, you would almost always want to include it.
- RewriteCond - This is what the actual rewrite conditions are, you can include multiple conditions each on a new line.
- RewriteRule - This is what rule is executed once one or more of the conditions above have been answered.
The following will redirect the URL while maintaining the subpages:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+\.)?old-domain.com$ [NC]
RewriteRule ^ http://new-domain.com%{REQUEST_URI} [R=301,L]
SSL forcing
Here is a sample piece of code which will redirect all traffic from port 80 on your website (the default port used when accessing websites via http://) to a specific URL, most commonly used to redirect website traffic to route via https://
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
An alternative method is as follows:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]
Note: These may cause a redirect loop, if the website software also tries to force a URL. Ensure that the software has the correct URL specified in the configuration.
Redirect a Subfolder to another
Redirect 301 /subfolder http://domain.com/new-subfolder
Common Errors
There are a number of common errors with redirects. The most common one that you might get while editing the .htaccess file is '500 Internal Server Error'. That is normally caused by incorrect syntax used in one or more lines of the file. You can simply revert recent changes or alternatively check the 'Errors' menu located in your cPanel to pinpoint the problematic line/s.
Infinite Loops
Bear in mind that manual redirects could interfere with other software redirects you have setup. For example, Wordpress as a software may redirect anything that is not DomainA to DomainA. If you redirect DomainA to DomainB then this will cause a recurring loop and error. This can also be caused by redirection plugins as well.
Note: If you are setting up the redirection in the .htaccess file manually you can always comment out the lines (using the symbol #) which will disable them.
Htaccess Precedence
.htaccess rules are followed in order from top to bottom. If you have two conflicting rules, the first one will take precedence.
Redirects and Aliases are an efficient way to redirect visitors from one web page to another. You can use these features to point your domain to a different one, with or without keeping the original website address in the URL bar of the browser.