Forum Moderators: open

Message Too Old, No Replies

canonical issues

         

kellyman

10:13 am on Mar 16, 2011 (gmt 0)

10+ Year Member



Hi Guys

Just wondering if someone can take a look at the below code to see if its set correctly, i personally have little knowledge of coding and am at the mercy of my web developer, the code is set just to deal with the canonical elements of www. non-www all is a window IIS 6

I am struggling with my home page at present and was wondering if the below was hindering somehow


$svr="http://www.$domain";

if ($requrl=="/index.php" or $requrl=="/index.htm" or $requrl=="/index.html" or $requrl=="/index.shtml" or $requrl=="/index.asp" or $requrl=="/index.aspx" or $requrl=="/default.asp" or $requrl=="/default.htm" or $requrl=="/index.cfm" or $requrl=="/index.pl"){
$doredirect=1;
}else if (substr($_SERVER["SERVER_NAME"],0,4)<>"www."){
$doredirect=1;
}else{
$doredirect=0;
}

if ($doredirect==1){ # echo $_SERVER["SERVER_NAME"].$requrl." redirect to $svr$requrl";
if ($requrl=="/index.php" or $requrl=="/index.htm" or $requrl=="/index.html" or $requrl=="/index.shtml" or $requrl=="/index.asp" or $requrl=="/index.aspx" or $requrl=="/default.asp" or $requrl=="/default.htm" or $requrl=="/index.cfm" or $requrl=="/index.pl"){
$correcturl=$svr;
}else{
$correcturl=strtolower("$svr$requrl");
}
header("HTTP/1.1 301 Moved Permanently");
header("Location: $correcturl");
exit;
}

g1smd

7:34 pm on Mar 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



"/index.php" or $requrl=="/index.htm" or $requrl=="/index.html" or $requrl=="/index.shtml" or $requrl=="/index.asp" or $requrl=="/index.aspx" or $requrl=="/default.asp" or $requrl=="/default.htm" or $requrl=="/index.cfm" or $requrl=="/index.pl"
This can be simplified using a regular expression which will parse a LOT faster:

"/(index|default)\.(php|s?html?|aspx?|cfm|pl)"

Ocean10000

2:11 pm on Mar 18, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



From just the looking at the code. I have no way of actually running to test it. It does look like it will do a 301 perm redirect for common canonical issues at the root level.
aka "http://www.example.com/index.php" would end up "http://www.example.com/"

But the following wouldn't be effected if my reading of the code is correct.
"http://www.example.com/subdir/index.php" would still show up and not be redirected.

kellyman

2:42 pm on Mar 18, 2011 (gmt 0)

10+ Year Member



But the following wouldn't be effected if my reading of the code is correct.
"http://www.example.com/subdir/index.php" would still show up and not be redirected.


Hi Ocean, absolutely correct, i was testing yesterday with my web guy and the way it pulls the forms that subdirectory is still there, we are looking at this as we speak, and putting in a fix, but its not as easy as just redirecting that out apparently

As i said im no coding expert but i have been complaining of homepage weakness for an age now do you think this is hindering me, i was thinking that 301 will lose a little link juice and if its hoping twice or more could be the result that i was page 1 for over 60 key phrases and now down to 14 after i had a coding error i asked about in another post [webmasterworld.com ]

Im just trying to reverse engineer the site and put things right

Thanks for the replys it really is appreciated

Ocean10000

2:09 am on Mar 19, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Here is a bit of code in C# which I think will do something similar to you goal as an example to merge multiple checks into one redirect.

//Assigns the current request URL to a local variable
System.Uri Address = this.Request.Url;

//gets the Hostname from the url
string host = Address.Host.ToLower();

//replaces hostname with the one we want to use.
//This makes it so we can force the domain name
//to be what we want always.
host = Address.AbsoluteUri.Replace(host, "www.example.com");

//creates a new URI with based on modified URI
Address = new Uri(host);

//This is where you do your checks for page level items
System.Text.RegularExpressions.Match regxmatch;
string Match = ""; //<-- this is what ever expression you are checking for

regxmatch = System.Text.RegularExpressions.Regex.Match(Address.AbsolutePath, Match, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
if (regxmatch.Success == true)
{
//You modify the Address URI class based on sucessfull match
//to repoint it where you need it to go
}


//Compares the Current modified Address to the Original and
//if they are different does a perm redirect.
if (Address.AbsoluteUri != this.Request.Url.AbsoluteUri)
{
this.Response.Status = "301 Moved Permanently";
this.Response.AppendHeader("Location", Address.AbsoluteUri);
this.Response.End();
return;
}