Redirecting clients to Alternate Pages

Situation 1

You wish to transparently redirect one page within your document root to another. The basis of this example is from an international site with Language Negotiation enabled, but where the desired behavior was to redirect unhandled languages to a default English-language page. Some of the code necessary to do this has been removed for clarity.

Solution

match URL into $ with ^/$
if matched then
   set SCRATCH:LOC      = http://www.example.com/en/homepage.html
   set Response         = 303
   set OUT:Location     = %{SCRATCH:LOC}
   set OUT:Content-Type = text/html
   set Body             = <!doctype html public "-W3C/DTD HTML 3.2EN">
                          <html><head><title>303 See Other</title></head>
                          <body><p>See <a href="%{SCRATCH:LOC}">here</a>.</p></body></html>
endif

(Note that the "set Body" section of the above example should be entered on a single line - it has been split here only for readability)

Description

This example will send a 303 ("See Other") response to requests for the root directory of a website only. These requests will then be redirected from index.html to /en/homepage.html whilst producing a standards-compliant redirect page for browsers that do not automatically redirect (an option available, albeit not enabled, for many current browsers).

Technical

For ease of maintainance, the location to redirect to is first stored in the temporary LOC variable. This saves having to have the same string appear twice in the rule, potentially breaking the rule if only one were updated.

Situation 2

You wish to operate a site where all content is stored on a secure server. To do this you have created two Virtual Servers, one standard and one secure, and wish to redirect all requests made to the standard server to the secure server instead, whilst keeping the details of the request intact.

Solution

match URL into $ with (.*)
if matched then
   set Response = 307
   set OUT:Location = https://intranet.example.com$1
   set OUT:Content-Type = text/html
   set Body = Go <a href="https://intranet.example.com$1">here</a> instead.
endif

Description

This example will route clients from a directory on the standard server to the same place on the secure server.

Technical

This example uses the 307 ("Moved Temporarily") response rather than the deprecated 302 ("Found"). Either of these should cause the client browser to attempt the original page for every request and not cache the fact that the page is moved. If instead, the browser should only attempt a single GET on the standard page and then honor the response, code 303 should be returned as detailed in the redirect example.

Note how the original request (including the initial "/") is mapped into $1, which is then postpended onto the destination location. The match could be written as "^/(.*)" to exclude the initial slash, allowing Location to be set to "https://intranet.example.com/$1" which probably has a lesser chance of being misunderstood.

Situation 3

You are a company providing hosting facilities, and would like to allow your Web Server to handle redirections to the domains you host, rather than relying on (and having to update) a large number of DNS entries.

Solution

match IN:Host into $ with ^.+$
if matched then
   set RESPONSE = 302
   set OUT:Location = http://www.domainhost.com/park.php?domain=%{IN:Host}
   set BODY = <a href="http://www.domainhost.com/park.php?domain=%{IN:Host}">Go here instead</a>
endif

Description

In this situation, we have a Virtual Server with a script that redirects requests for a particular domain to a dynamically specified document root.

Techincal

This rule matches any Host header of at least one character (which will correctly ignore HTTP/1.0 requests without a Host header), then redirects the request to the specified PHP script with the hostname as a parameter. This script is then responsible for matching a hostname to a document root.

Content Manager [Administrator] 07 March 2006  Permalink 2 comments  

Comments:

This public messageboard is not a forum for technical support. To report technical support problems, please contact our dedicated Support team using the instructions at the bottom of this page.

Comment from: MIke [Visitor]
Hi

How do you do a permanent redirect from http://domain.com to http://www.domain.com? I need to do this so that search engines don't index my site twice.

Thanks
Mike
Permalink 15 November 2007 @ 08:00
Comment from: Teenager [Visitor] · http://teenssex.name/
Mike use Aliases : Aliases are a way of specifying 'alternative web addresses' for a website. Specify a list of additional host names for this website, separated by spaces. www.domain.com
Permalink 18 January 2008 @ 00:45
Leave a comment ...
Your email address will not be displayed.
Your URL will be displayed.
This public messageboard is not a forum for technical support. To report technical support problems, please contact our dedicated Support team using the instructions at the bottom of this page.
Options:
 
(Line breaks become <br />)
(Set cookies for name, email & url)
Download Free Trial

Recent Articles

Other Resources



www.zeus.com