【问题标题】:Rewrite rules for multiple files重写多个文件的规则
【发布时间】:2015-03-28 10:44:46
【问题描述】:

我们刚刚推出了一个新网站,发现 WebMaster 工具中有多个 Google 错误需要我们修复。我们在启动前使用重定向 301 重定向了主要页面,但希望为不存在文件的字符串/目录/等重写 500 多个错误。

错误如下所示:

  • /product.php?id=143(多个 ID)
  • /page.php?id=109(多个ID)
  • /product_42-productname.htm(多个产品名称)

我们可以做一个包罗万象的重定向,任何以 /product 开头的页面都转到一个页面吗?

我们正在运行一个 Wordpress 网站,因此当前的 htaccess 有:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>'

我们是否需要将其作为该语句的一部分或稍后包含在 htaccess 文件中?

我们已经尝试了很多选项,但不想列出 htaccess 中的每个项目,只是不知道如何轻松解决这个问题???

非常感谢任何帮助。

【问题讨论】:

    标签: php wordpress .htaccess mod-rewrite redirect


    【解决方案1】:

    您可以捕获有问题的“产品”链接并重定向到您的主页index.php。该规则将出现在您的 wordpress 规则之前,如下所示。

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^(product|page) /index.php? [R,L]
    
    RewriteRule ^index\.php$ - [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    

    无论它带有什么查询参数(id),该规则都会捕获链接。

    【讨论】:

    • Thx Ravi,这可行,但最后会留下一个尾随 ID。例如。 /product.php?id=143 变为 www.example.com.au//?id=143。删除ID查询容易改吗?
    • 是的,很简单。只需将 index.php 更改为 index.php?如我的更新所示。您也可以将 R 更改为 R=301,以防您想做 301 永久重定向。
    猜你喜欢
    • 2019-12-12
    • 2013-06-21
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 2016-08-13
    相关资源
    最近更新 更多