Transparently redirecting clients from http://domain.com/ to http://www.domain.com/

Here's how to accomplish vanity domain names within ZXTM. Many popular websites will respond to a http://domain-name.com/ request, but will issue a 301 re-direct to http://www.domain-name.com/. Let's demonstrate how ZXTM can quickly and easily accommodate this requirement, including transparently re-writing any URL request.

# Cache Host header being issued by the client
$host = http.getHeader( "Host" );
# This line passes both the path and query string to $url, meaning that
# a request for /path/to/output?is-here.php is placed into the below variable.
$url = http.getRawURL();
# log.info( "url: ".$url ); # Optional log output
# Use a Perl Compatible regular expression to match our domain name.
if( string.regexmatch( $host, "^domain-name$" ) ) {
# Re-write the variable cached in $host to include a prefix of 'www.'
$location = string.regexsub( $host, "domain-name.com", "www.".$host );
# Issue a permanent re-direct to the client (Google requires a 301 response code to
# ensure a request for http://domain-name/ is included within
http.sendResponse( "301 Moved Permanently", "text/html", "",
# Catenate the newly created $location variable with the original $url query request
"Location: http://".$location.$url);
}

Here's our final trafficscript in full vim syntax glory:

Chris Buckley [Zeus Support] 04 August 2006 Bookmark with del.icio.us Post this article to Digg Post this article to reddit Post this article to Facebook Tweet this article 3 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: vinny foreman [Visitor] · http://www.redheadmedia.co.uk
Hi I'm trying to achieve the above on a site that is hosted on a Zeus server. I'm not able to use the .htaccess file. I have tried the above script but it doesn't work. I have searched a great length on the internet for an answer to this question and have found a number of solution, none of which work.

1. Can someone please tell me how to achieve a simple 301 redirect from non www to www equivalent.
2. what file should this be in and where
3. are there any necessary permissions on this file

thanks
Vinny
Permalink 08 July 2008 @ 20:44
Comment from: Alex Smith [Visitor]
Is it possible to do this using the Rule Builder? It seems that our licence doesn't allow us to use TrafficScript stuff.

Thanks,

Alex
Permalink 23 September 2009 @ 00:39
Comment from: Yvan Seth [Zeus Dev Team] · http://knowledgehub.zeus.com/
Hi Alex,

The exact functionality presented in the example above can be replicated with the Rule Builder. However, the Rule Builder approach is much harder to expand out to more complicated scenarios.

For example, say you have a Virtual Server listening for requests to the domain www.foo.com. Use the Rule Builder to create a rule with just the Change HTTP site action (in the HTTP only section.) Set the site for this action to www.foo.com. Add the rule to your VS as a request rule.

Now any request that comes into that VS for a domain other than than www.foo.com will be sent a 301 redirect using the desired hostname (preserving the original request's URL path.)

If you have more complex setups, such as single VSs that handle multiple domains you may be able to work something out using various combinations of conditions. This is getting into the realm of functionality that Traffic Script was designed for though and if such be your requirements a feature upgrade would be recommended. For example, with Traffic Script you could trivially implement redirects that perform actions along the lines of mapping name.foo.com/url to foo.com/name/url.

Hope that helps,
Yvan
Permalink 23 September 2009 @ 08:15
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)

Recently...

Other Resources