【问题标题】:.htaccess and seo friendly search query string.htaccess 和 seo 友好的搜索查询字符串
【发布时间】:2012-01-05 13:51:57
【问题描述】:

我对.htaccess 真的很陌生。我想知道如何为搜索字符串和选定的过滤器创建一个复杂的 seo 友好 url。

我目前正在使用以下代码进行搜索。

ReWriteRule ^search/(.*)/(.*)   ?module=search&q=$1&page=$2 [L]
ReWriteRule ^search/(.*)        ?module=search&q=$1 [L]

在添加过滤器选项时,它开始成为一场噩梦。考虑以下过滤器;

缩写:无、姓名、日期 + ASC、DESC。 类别:无,类别 ID。 搜索范围:全部、标题、消息、作者。

选择所有过滤器后,我们的地址将是;

原始: www.site.com/?module=search&q=test&shortBy=name_ASC&catID=1&searchIn=title

搜索引擎友好: www.site.com/search/test/name_ASC/1/title/

现在这一点都不复杂,但我只想了解事情的运作方式。 即使没有选择,我是否必须将所有过滤器应用于 URL?这会无缘无故地创建更长的 URL,所以我相信必须有另一种方法。

如何在 .htaccess 中定义规则?在我的示例中,我在 .htaccess 文件中至少编写了 2 条规则(第二条用于分页)。

我现在只想做这样的事情^search/(.*)/(.*) ?module=search&$1=$2 [L],但这看起来并不优雅。

如果你能帮我解决这个问题,我会很高兴。如果您也能像我一样将您的一些经验分享给 .htaccess 新手,我将不胜感激。

【问题讨论】:

  • 这后面是否有页面,例如请求提交给index.php

标签: .htaccess mod-rewrite seo


【解决方案1】:

将以下规则放在您的 .htaccess 文件顶部(在您网站的根目录中),然后选择以下选项之一,根据您的需要对其进行修改,并将其放在您的 .htaccess 文件中。

第一个选项将匹配最多的 url,不推荐。第二个只会匹配 5 个目录的 url,但需要所有参数,所以我推荐第 3 个。

虽然下面的模式可能看起来不太优雅,但它是The Definitive Guide to Apache mod_rewrite 中的特色解决方案

有关更多详细信息,请查看apache docs 或一些examples 或此post

在所有情况下,我都假设请求转到 index.php,因此您应该修改它以匹配实际页面。

此部分应位于所有 3 个选项的 .htaccess 文件的顶部

#for all 3 options these 2 lines should be at the top of the .htaccess file
RewriteEngine On
RewriteBase /

选项1

#1 if all parameters are optional, and you page (index.php) can handle blank parameters use
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/$ index.php?module=$1&q=$2&shortBy=$3&catID=$4&searchIn=$5 [L]

选项2

#2 if all parameters are required use
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?([^/]+)/([^/]+)/$ index.php?module=$1&q=$2&shortBy=$3&catID=$4&searchIn=$5 [L]

选项3

#3 if the module is required and other parameters are optional, then this is better
RewriteCond %{REQUEST_URI} ^/(search|produce_detail|other_modules_here)/
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/$ index.php?module=%1&q=$1&shortBy=$2&catID=$3&searchIn=$4 [L]

【讨论】:

  • 问题是这不是唯一的页面。我想选择选项 1。我可以处理空白参数,但我绝对不希望在 URL 上看到它们,例如 = search/query//4/news 如果是这种情况,使用选项 1,它看起来并不优雅。我会试一试,非常感谢。
猜你喜欢
  • 1970-01-01
  • 2019-10-31
  • 1970-01-01
  • 1970-01-01
  • 2012-09-02
  • 2011-08-13
  • 2013-02-03
  • 1970-01-01
  • 2015-04-17
相关资源
最近更新 更多