分享

PHP 301 Redirect Function | PHP 302 Redirect ...

 tianhao 2009-12-21
 PHP redirect function (301 / 302)

PHP redirect function (301 / 302)

I'm often asked how to do 301 redirects in PHP. It's not hard, but it's a pain having to remember the exact syntax every time. Here's a little function I wrote for Jojo CMS for making redirects easy.

Usage

Add one line of code whenever you need to do a redirect.

301 redirects...

redirect('http://www.');
or
redirect('http://www.', 301);

302 redirects...

redirect('http://www.', 302);

The function

You will need to include this code in your PHP script to be able to use this redirect function.
function redirect($url, $type=301)
{
if ($type == 301) header("HTTP/1.1 301 Moved Permanently");
header("Location: $url");
echo 'This page has moved to <a href="'.$url.'">'.$url.'</a>';
exit();
}

Jojo users

This function is available as part of Jojo CMS. Simply place "Jojo::" before the function call eg...
Jojo::redirect('http://www.');

Not so hard

There we go. A little function that makes a reasonably simple job very simple. It's saved me lots of time having to remember the exact syntax for a 301 redirect.

Feel free to send a link my way if you find this useful.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多