homepage Welcome to WebmasterWorld Guest from 54.227.12.4
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Google / Google SEO News and Discussion
Forum Library, Charter, Moderators: Robert Charlton & aakk9999 & brotherhood of lan & goodroi

Google SEO News and Discussion Forum

    
What code can we use to redirect non-www to www
johan




msg:776327
 8:38 am on Oct 25, 2005 (gmt 0)

What code can we use to redirect non-www to www on a page that would work. PHP and an ASP VBScript would be useful for anyone on Windows hosting who don’t have access to the server control.

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);
}

 

vBMechanic




msg:776328
 2:32 pm on Oct 25, 2005 (gmt 0)

PHP

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;
}

SebastianX




msg:776329
 3:12 pm on Oct 25, 2005 (gmt 0)

The C code above applied to ASP results in a 302 response, because the method Response.Redirect(location) does that by default. This code (from the top of my head, not tested) should do the trick if you suffer from Windows:

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]

johan




msg:776330
 4:02 pm on Oct 25, 2005 (gmt 0)

I have both PHP and ASP (VBScript) running off a Windows server with no access to .htaccess

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).

encyclo




msg:776331
 4:07 pm on Oct 25, 2005 (gmt 0)

For the parse error, the above is missingf a semi-colon at the end of the line:

else if ($PHP_SELF=="/index.php")[red];[/red]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Google / Google SEO News and Discussion
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved