【问题标题】:Redirect 301 & Mod Rewrite Issues重定向 301 和 Mod 重写问题
【发布时间】:2010-12-31 03:53:45
【问题描述】:

我有一个在线商店,例如,位于此处:hxxp://domain.com/store/

在商店目录的 .htaccess 文件中,我有这个:

Options +FollowSymlinks
RewriteEngine on
RewriteBase /store/
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/store/$1 [R=301]
RewriteRule ^/?$ directory.php
RewriteRule ^search/?$ search.php
RewriteRule ^category/([a-zA-Z0-9\!\@\#\$\%\^\&\*\(\)\?\_\-\ ]+)/?$ product.php?CategoryID=$1 [QSA]
RewriteRule ^([a-zA-Z0-9\!\@\#\$\%\^\&\*\(\)\?\_\-\ ]+)/?$ product/detail.php?ProductID=$1 [QSA]

效果很好!

我的问题(问题)是我现在需要将 /store/ 目录更改为 /shop/ ,这似乎很简单。但是,我需要设置正确的 301 重定向,这样我就不会失去我可能拥有的任何 SE 排名。

在这种情况下设置 301 重定向的最佳方法是什么?

我找到的唯一解决方案是为商店中的每个类别、产品等设置一个重定向 301。比如这样。

Redirect 301 /store/category/sample-widgets/ hxxp://www.domain.com/shop/category/sample-widgets/

这可以工作并且可以满足我的需要,但是...地址栏中的 URL 显示如下:hxxp://www.domain.com/shop/category/sample-widgets/?CategoryID=sample-小部件

我不知道为什么或如何删除查询字符串。

请帮忙。谢谢。

【问题讨论】:

    标签: .htaccess mod-rewrite redirect http-status-code-301


    【解决方案1】:

    您可以使用 PHP 脚本处理重定向来处理 301 错误。

    在您的 .htaccess 文件中,您将添加以下规则:

    Redirect 301 /error301.php

    创建文件error301.php:

    <?php
    
    $redirects = array('/path/to/old/page/' => '/path/to/new/page/',
                       '/store/category/sample-widgets/' => '/shop/category/sample-widgets/');
    
    if (array_key_exists($_SERVER['REQUEST_URI'], $redirects))
    {
        $dest = 'http://'.$_SERVER['HTTP_HOST'].$redirects[$_SERVER['REQUEST_URI']];
        header("Location: {$dest}", TRUE, 301); // 301 Moved Permanently
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用简单的Redirect 指令,例如:

      Redirect 301 /store/ /shop/
      

      或者,如果您也想使用 mod_rewrite,则需要更改当前规则,因为您不能再使用基本 URL /store/

      RewriteEngine on
      RewriteCond %{HTTP_HOST} !=www.example.com
      RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
      RewriteRule ^store/?([^/].*)?$ /shop/$1 [L,R=301]
      RewriteRule ^shop/?$ directory.php
      RewriteRule ^shop/search/?$ shop/search.php
      RewriteRule ^shop/category/([a-zA-Z0-9!@#$%^&*()?_\-\ ]+)/?$ product.php?CategoryID=$1 [QSA]
      RewriteRule ^shop/([a-zA-Z0-9!@#$%^&*()?_\-\ ]+)/?$ product/detail.php?ProductID=$1 [QSA]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多