Forum Moderators: coopster

Message Too Old, No Replies

Performing a 301 redirect in PHP (instead of a 302)

Fixing a SEO problem of my CMS, that generates 302 redirect

         

zapotex

7:48 pm on Dec 1, 2011 (gmt 0)

10+ Year Member



Hi everyone!

I'm using coppermine gallery (a CMS for image hosting) and when a user adds the picture to the favorites, a page (addfav.php) is called, with picture id ("pid") and referrer url ("referer") as parameters.

The addfav page saves the picture in the favorites, then redirects the user to the picture. Unfortunately the redirect is 302, which search engines do not like.

Here is the relevant parts of the code of addfav.php:


...

$pid = $superCage->get->getInt('pid');

...

$ref = $CONFIG['site_url'] . (!empty($CPG_REFERER) ? $CPG_REFERER : "displayimage.php?pid={$pid}");
$ref = str_replace('&', '&', $ref);

//What is done above is essentially, just a $_GET of pid and referer

...

$header_location = (@preg_match('/Microsoft|WebSTAR|Xitami/', getenv('SERVER_SOFTWARE'))) ? 'Refresh: 0; URL=' : 'Location: ';
header($header_location . $ref);
pageheader($lang_common['information'], "<meta http-equiv=\"refresh\" content=\"1;url=$ref\">");
//I think the line above is the key. It does a 302 redirect I think, but I'm not an expert
msg_box($lang_common['information'], $lang_rate_pic_php['rate_ok'], $lang_common['continue'], $ref);
//Apparently the line above is a msg_box, but I don't see any when I click on "add to favorites"
pagefooter();
//I don't know what pagefooter is, but I don't think it matters for the redirect.


Can anybody help me? How to transform this into a 301 redirect?

Thanks a lot!

Best

g1smd

7:52 pm on Dec 1, 2011 (gmt 0)

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



It does a meta refresh. That is neither a 301 or 302 redirect.

Instead of sending a meta refresh in the HTML body, use the PHP HEADER directive to send a 301 redirect before the start of the HTML page.

zapotex

7:59 pm on Dec 1, 2011 (gmt 0)

10+ Year Member



Awesome! Thanks so much! I will do that!

PS: I said 302 redirect because for SEO softwares and header checkers (such as this [seoconsultants.com]), it looks like a 302.

I will try what you suggest! Thanks a lot!

zapotex

8:30 pm on Dec 1, 2011 (gmt 0)

10+ Year Member



Hi g1smd,

I tried this code:

header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$ref);


The addfav.php file is executed correctly and the browser is redirected to the photo as expected, but... the header checker still gives me a 302. How can it be? I explicitly sent a header with "HTTP/1.1 301 Move Permanently"...

I'll look for more info about the header function.

Thanks again for pointing me the right direction!