Link to home
Start Free TrialLog in
Avatar of drbill1
drbill1Flag for United States of America

asked on

Prepend www to URL using PHP

I have a WordPress blog hosted on IIS and I've been trying to get ISAPI_Rewrite to work so that I can prepend www to the URL. It isn't working after 3 hours, so I just want to use PHP redirect to get it working. I've done it before with ASP (which I've included) but I don't know PHP well enough to translate this code. Can you do it?
<%
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
%>

Open in new window

Avatar of xBellox
xBellox

something like this:

<?
if (strpos($_SERVER['SERVER_NAME'], "www")===false)
    header("Status: 301")
    header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']");
}
?>

Open in new window

I forgotthe ;  at the end of header("Status: 301") and "exit;" in place of  "Response.End"

... The correct should be:

<?
if (strpos($_SERVER['SERVER_NAME'], "www")===false)
    header("Status: 301");
    header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']");
    exit;
}
?>
 
or 
 
<?
if (strpos($_SERVER['SERVER_NAME'], "www")===false)
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']");
    exit;
}
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of xBellox
xBellox

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Chad Smith
Chad Smith
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeah, 3 little typos in just one question... Might be the record :P

Thanks for the help :)
Avatar of drbill1

ASKER

Worked perfect with the corrected typos, thanks!