Forum Moderators: Robert Charlton & goodroi
In C it would look something like this:
string url = Request.RawUrl;if (url.IndexOf("http://www") >= 0)
{
Response.Redirect(url);
}else
{
url = "http://www" + url;
Response.Redirect(url);
}
if (!preg_match("/^www.example/i",$_SERVER[HTTP_HOST]) ) {
header("HTTP/1.1 301 Moved Permanently");
if (strlen($_SERVER['QUERY_STRING'])>0)
{
header("location:http://www.example.com$PHP_SELF?$_SERVER['QUERY_STRING']");
}
else if ($PHP_SELF=="/index.php")
{
header("location:http://www.example.com/");
}
else
{
header("location:http://www.example.com$PHP_SELF");
}
exit;
}
DIM url,server
server = "www.example.com"
' add more if the URI contains a query string:
url = "http://" & server & Request.ServerVariables("URL")
if Request.ServerVariables("SERVER_NAME") <> server then
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", url
Response.End
end if
If you have a real host, put this in your root's .htaccess file and don't bother with redirects in scripts:
# redirect permanent domain.tld/* to www.domain.tld/*
RewriteEngine On
RewriteCond %{HTTP_HOST}!^www\.example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
The ASP sites are fixed but with PHP I dont really know at all I keep getting:
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in D:\.....Line 6 (that line 5 in the PHP sample in the above post).