Sometimes it is required to redirect a User visiting the website.
A redirect changes the URL in the browser from one URL to another.
There are various methods on how to accomplish this.
A simple way to redirect is to use htaccess.
For a more general .htaccess guide visit the Knowledgebase.
In this simple example, we just redirect the Clients Web-Browser.
RewriteEngine On
RewriteRule ^ http://web.example.com [R=301,L]
In this example we simply redirect a Client when the
URL is www.example.com to example.com.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$ http://example.com/$1 [R,L]
The following directives check for a variable called HTTPS that gets
set whenever SSL is used. If this is not set (non-ssl connection), the
client gets Redirected to SSL (https) for the same Hostname + URL path.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !^on$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
An optional number may be added onto a R flag to specify a redirect code. By default a 302 is sent in a redirect response. (use R=XXX, where XXX is the response Code. Example: R=307).
301 | Moved Permanently | Resource is no longer accessible under URL, use new URL (permanent) |
302 | Found | URL resides temporarily under new resource, continue to use this URL |
303 | See Other | Response resides elsewhere and should be retrieved using a GET; |
307 | Temporary Redirect | URL resides elsewhere; use the new location temporarily for requests |
Sometimes a quick redirect from a directory (via index.php) or php script is required,
In this example we redirect browser accesing this php script to www.example.com
<?php
header("Location: http://www.example.com/"); /* Redirect browser */
?>