Knowledge Base

Website: Redirecting

Overview

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 web.example.com

 

 

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

RewriteEngine On
RewriteRule ^ http://web.example.com [R=301,L]

Example 1: Redirecting from www.example.com

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]

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 www.example.com

<?php
header("Location: http://www.example.com/"); /* 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: