.htaccess 和 seo 友好的搜索查询字符串

发布于 2024-12-17 17:14:05 字数 961 浏览 0 评论 0原文

我对 .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

Seo 友好: www.site.com/search/test/name_ASC/1/ title/

现在这根本不复杂,但我只是想了解事情是如何工作的。 即使没有选择,我是否也必须将所有过滤器应用到 URL?这会无缘无故地创建更长的 URL,所以我相信必须有另一种方法来解决这个问题。

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

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

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

I am really new to .htaccess. I was wondering how to create a complex seo friendly urls for the search string and selected filters.

I'm using following code currently for the search.

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

When it comes to adding filter options it starts to be a nightmare. Consider following filters;

Short by: none, name, date + ASC, DESC.
Category: none, category ID.
Search In: All, title, Message, Author.

When all filters are selected our address would be;

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

Seo Friendly : www.site.com/search/test/name_ASC/1/title/

Now that's not complex at all but I just want to understand the how things work.
Do I have to apply all the filters to URL even if they are not selected? This would create longer URLs for no reason so I believe there must be another approach to this.

How do I define the rule in .htaccess? As in my example, I am writing at least 2 rules in my .htaccess file (2nd one is for pagination).

All I could think right now to do something like this ^search/(.*)/(.*) ?module=search&$1=$2 [L] but this doesn't look elegant.

I will be glad if you could help me out with this problem. I will be grateful if you could also share some of your experiences to a .htaccess newbie like I am.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事与诗 2024-12-24 17:14:05

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

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

尽管下面的模式可能看起来不太优雅,但它是 Apache 权威指南中的特色解决方案mod_rewrite

有关更多详细信息,请参阅 apache 文档 或一些示例 或这个帖子

在所有情况下,我假设请求发送到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]

Put the rules below at the top of your .htaccess file (in the root directory of your site) and then select one of the options below, modify it to your needs and place in after in your .htaccess file.

The first option will match the most urls and is not recommended. The second will only match urls with 5 directories, but requires all the paramters, so I recommend the 3rd.

Though the pattern below may not look elegant, it is a featured solution in The Definitive Guide to Apache mod_rewrite

For more details look at the apache docs or some examples or this post

In all cases, I am assuming that the requests go to index.php, so you should modify this to match the actual page.

This section should go at top of .htaccess file for all 3 options

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

Option1

#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]

Option2

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

Option3

#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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文