View Single Post

  #4 (permalink)  
Old 09-07-2008, 09:32 PM
ghost's Avatar
ghost ghost is offline
Wannabe Geek
 
Join Date: Dec 2007
Location: Ennis
Posts: 176
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
ghost will become famous soon enough
Default

Hostname Redirect non-www to www
This code should be inserted into a global include file or any script which is executed for every page on the site before the page output begins:

For asp

Code:
<%
If InStr(Request.ServerVariables("SERVER_NAME"),"www") = 0 Then
    Response.Status="301 Moved Permanently"
    Response.AddHeader "Location","http://www."
        & Request.ServerVariables("HTTP_HOST")
        & Request.ServerVariables("REQUEST_URI")
    Response.End
End if
%>
For php

Code:
<?php
if (substr($_SERVER['HTTP_HOST'],0,3) != 'www') {
    header('HTTP/1.1 301 Moved Permanently'); 
    header('Location: http://www.'.$_SERVER['HTTP_HOST']
    .$_SERVER['REQUEST_URI']);
}
?>
Compliments of

301 Redirects in Apache .htaccess, IIS, PHP, ASP and ColdFusion - Beyond Ink
Reply With Quote