如何在PHP中将303重定向更改为301重定向

时间:2012-02-22 18:21:43

标签: php

我使用这个Joomla 1.5插件进行重定向。效果很好,但它返回“303 see other”重定向状态,而不是SEO友好301

对于插件的下面代码是否有任何可以使其成为301重定向?

<?php
/**
 * JRedirect plugin
 * 
 * @author Ross Farinella
 * @version 1.0.0
 * @license GPL
*/

defined( '_JEXEC' ) or die( 'Restricted access' );

global $mainframe;
$mainframe->registerEvent('onAfterRoute', 'plgSystemCheckJRedirect');

/**
 * Checks to see if the current URL being requested is one of the "special" URLs 
 *  and redirects the application as necessary
 */
function plgSystemCheckJRedirect() 
{
global $mainframe;

// get the plugin parameters
$tmp = JPluginHelper::getPlugin("system","jredirect");
$params = new JParameter($tmp->params);

// get the current URI
$current = JRequest::getURI(); // "/something.html"

$urls = $params->get('urls');
$urls = explode("\n",$urls);
foreach($urls as $url) 
{
    // get the user-entered urls
    list($toCheck,$toRedirect) = explode("|",$url);

    // check if we're at this url
    if($current == "/".$toCheck) {
        // do the redirect
        $mainframe->redirect($toRedirect);
    }
}
}


?>

1 个答案:

答案 0 :(得分:3)

根据Joomla documentation(或更确切地说,the source),您应该可以通过更改

来实现
$mainframe->redirect($toRedirect);

$mainframe->redirect($toRedirect,'','message',true);

所以:

<?php
/**
 * JRedirect plugin
 * 
 * @author Ross Farinella
 * @version 1.0.0
 * @license GPL
*/

defined( '_JEXEC' ) or die( 'Restricted access' );

global $mainframe;
$mainframe->registerEvent('onAfterRoute', 'plgSystemCheckJRedirect');

/**
 * Checks to see if the current URL being requested is one of the "special" URLs 
 *  and redirects the application as necessary
 */
function plgSystemCheckJRedirect() 
{
global $mainframe;

// get the plugin parameters
$tmp = JPluginHelper::getPlugin("system","jredirect");
$params = new JParameter($tmp->params);

// get the current URI
$current = JRequest::getURI(); // "/something.html"

$urls = $params->get('urls');
$urls = explode("\n",$urls);
foreach($urls as $url) 
{
    // get the user-entered urls
    list($toCheck,$toRedirect) = explode("|",$url);

    // check if we're at this url
    if($current == "/".$toCheck) {
        // do the redirect
        $mainframe->redirect($toRedirect,'','message',true);
    }
}
}


?>