Knowledge Base

Website: Redirecting


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.

Redirects Using .htaccess

A simple way to redirect is to use htaccess. 
For a more general .htaccess guide visit the Knowledgebase.

Example 1: Redirect to



In this simple example, we just redirect the Clients Web-Browser.

RewriteEngine On
RewriteRule ^ [R=301,L]

Example 1: Redirecting from

In this example we simply redirect a Client when the 
URL is to

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$$1 [R,L]

Example 2: Redirect to SSL

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]

Notes on Redirects using .htaccess

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).

301Moved PermanentlyResource is no longer accessible under URL, use new URL (permanent)
302FoundURL resides temporarily under new resource, continue to use this URL
303See OtherResponse resides elsewhere and should be retrieved using a GET; 
307Temporary RedirectURL resides elsewhere; use the new location temporarily for requests

Redirect using PHP

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

header("Location:"); /* Redirect browser */
Please rate this article to help us improve our Knowledge Base.

0 0

a brand of Sebastian-Wilhelm Graf

Premium IT-Services

Proud Member: