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