homepage Welcome to WebmasterWorld Guest from 54.204.182.118
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP Redirect script
kellyman



 
Msg#: 4474217 posted 4:18 pm on Jul 10, 2012 (gmt 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;
}

 

eelixduppy

WebmasterWorld Senior Member eelixduppy us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4474217 posted 5:16 pm on Jul 10, 2012 (gmt 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.

kellyman



 
Msg#: 4474217 posted 7:13 pm on Jul 10, 2012 (gmt 0)

Thank you

although I don't know too much about code i could see duplication there so many thanks for clarifying for me

matrix_jan



 
Msg#: 4474217 posted 7:40 pm on Jul 10, 2012 (gmt 0)

Else ifs don't like to be consolidated :)

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4474217 posted 9:43 pm on Jul 10, 2012 (gmt 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.

kellyman



 
Msg#: 4474217 posted 10:46 pm on Jul 10, 2012 (gmt 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

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4474217 posted 11:00 pm on Jul 10, 2012 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
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