Welcome to WebmasterWorld Guest from 54.224.46.77

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP Redirect script

     
4:18 pm on Jul 10, 2012 (gmt 0)

Full Member

5+ Year Member

joined:Oct 11, 2010
posts: 226
votes: 0


Hi Guys

Posted a while back in regard to a htaccess script for a new domain

want to kind of ask the same question but with PHP script

I have a site which is going to be directed in the coming weeks to a new site, the below code should solve the Canonical issues as well as any capitalized letters and the forward stroke at the end of URL's i can kind of get my head around this and all looks fine but below is the full code for doing this but i cannot help but think there is a duplication in there some where.

is it possible someone can have a look and let me know if this does all that it should as simply as possible as it looks to me theres 2 occasions where is asks for lower case


if ($requrl=="/" and substr($_SERVER["SERVER_NAME"],0,4)<>"www."){

$correcturl=strtolower($svr);
header("HTTP/1.1 301 Moved Permanently");
header("Location: $correcturl");
exit;

}else if (substr($_SERVER["SERVER_NAME"],0,4)<>"www." or ($requrl<>"/" and substr($requrl,-1)=="/") or $requrl<>strtolower($requrl)){

$correcturl=strtolower(rtrim("$svr$requrl","/"));
header("HTTP/1.1 301 Moved Permanently");
header("Location: $correcturl");
exit;
}
5:16 pm on July 10, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


You should try to avoid code duplication as much as possible. Here, the only thing changing is the value of
$correcturl
. Everything else should be consolidated:


$serverNamePrefix = substr($_SERVER['SERVER_NAME'], 0, 4);
$correctUrl = '';

if($serverNamePrefix != 'www.' && $requrl == '/') {
$correctUrl = strtolower($svr);
} else if($serverNamePrefix != 'www.' || ($requrl != '/' && substr($requrl, -1) == '/') || ($requrl != strtolower($requrl))) {
$correctUrl = strtolower(rtrim($svr.$requrl, '/'));
}

header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $correctUrl);
exit;


something like that, perhaps.
7:13 pm on July 10, 2012 (gmt 0)

Full Member

5+ Year Member

joined:Oct 11, 2010
posts: 226
votes: 0


Thank you

although I don't know too much about code i could see duplication there so many thanks for clarifying for me
7:40 pm on July 10, 2012 (gmt 0)

Preferred Member

5+ Year Member

joined:Mar 27, 2010
posts:423
votes: 0


Else ifs don't like to be consolidated :)
9:43 pm on July 10, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


You'll need to invoke the PHP script from a RewriteRule configured as an internal rewrite.

If you mistakenly redirect to the PHP script you will generate an unwanted multiple step rediction chain.


Make sure the target URL of the PHP-generated redirect includes the full protocol and domain as well as the path.
10:46 pm on July 10, 2012 (gmt 0)

Full Member

5+ Year Member

joined:Oct 11, 2010
posts: 226
votes: 0


Thats not the first time i read that, its kind of hard as i know that there is an error but not experienced to rectify what he has done, I can find example of individual codes such as site to site redirect and canonical , and then the trailing slash

however i cannot find the correct answer to all which is

canonical www. and non www.
remove trailing slash
redirect any upper case urls

as those above are being addresed now and i know when i move the site they will need to be actioned.

can you point me in an example of exactly what i need as i cannot afford to get this wrong and unfortunately I dont know enough
11:00 pm on July 10, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


preg_replace()
allows you to strip the slash.

strtolower()
will fix the casing.

The target URL specified in the PHP-generated redirect must include protocol and domain. This will take care of the non-www/www problem.

Thorough testing is important. I assume you do have a test sub-domain? Test in a sub-domain not in a folder.