【问题标题】:Infinite redirect loop - rewriting URLs无限重定向循环 - 重写 URL
【发布时间】:2013-01-25 16:11:21
【问题描述】:

在为 SEO 目的实施 URL 重写时,我遇到了无限重定向循环。

示例网址

`<a><?php echo make_store_name_url($store_id); ?><?php echo $store_name; ?></a>`

我有一个重写动态 url 的函数 - 下面是一个例子

function make_store_name_url($store_id)

{
     //build the keyword rich url
     $url = SITE_URL . '/store/' . $store_id .'/';

    //return the URL
    return $url;
}

//function to redirect using 301
function fix_store_name_url()
{
   $proper_url = get_proper_store_name_url();

   if(SITE_URL . $_SERVER['REQUEST_URI'] != $proper_url)
   {
      header('HTTP/1.1 301 Moved Permanently');
      header('Location: ' . $proper_url);
      exit();
   }
}

function get_proper_store_name_url()
{
  $store_id = $_GET["store"];  
  $proper_url = make_store_name_url($store_id);
  return $proper_url;
}

最后我在 htaccess 中的行重写了。请注意,当不使用重定向时,重写工作正常。

RewriteRule ^store/([0-9]+)/$ /store_selection.php?store=$1 [R=301,L]

不确定我的无限重定向循环出了什么问题。任何帮助将不胜感激。

【问题讨论】:

标签: php .htaccess url-rewriting seo infinite-loop


【解决方案1】:

为什么不用 RewriteLog 来调试重写规则呢?

# Roll your own Rewrite log
# Log details via scale of 1 to 9
# 1 = few details, 5 = enough details, 9 = too much detail
RewriteEngine On
RewriteLog "/your/path/rewrite.log"
RewriteLogLevel 5

http://perishablepress.com/roll-your-own-apache-rewrite-log/

【讨论】:

    【解决方案2】:

    将您的 .htaccess 更改为:

    RewriteRule ^store/([0-9]+)/$ /store_selection.php?store=$1 [QSA,L]
    

    这只是在内部重写 URL,同时仍向用户显示原始 URL。您已将其设置为执行 301 重定向,因此用户点击的 URL 是 /store_selection.php?store=id 即无限循环的原因。

    【讨论】:

    • 我仍然得到一个重定向循环。我已经使用 fiddler 检查了标题,并且重写 url 的位置是正确的,但是我仍然得到 21 301 重定向。 @Birla
    • @user2058618 它非常适合我。但是,如果我将QSA 改回R=301,它就会陷入无限循环。您的浏览器可能存在缓存问题。
    猜你喜欢
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多