首页 > 解决方案 > 如何通过允许 mod_rewrite 作为查询字符串和页码进行 .htaccess 搜索?

问题描述

我的原始文件是一个名为search.php.

通常,搜索功能将是:

mydomain.com/search.php cid=&type=search&q=keyword+text&page=2

但是,是否可以将其重写为mydomain.com/all.html?q=keyword+text&page=[1-anypage]?

我试图将此代码放入.htaccess:

RewriteRule ^all.html?q=([^.*]+)&p=([0-9]+)$ search.php?cid=&type=search&q=$1&p=$2 [L]

但出了点问题。

请帮我找到解决方案。谢谢。

标签: php.htaccesssearchmod-rewrite

解决方案


是的,它可能,但您无法匹配 RewriteRule 模式中的查询字符串,您将需要使用RewriteCond指令来检查%{QUERY_STRING}类似以下内容

RewriteEngine On

RewriteCond %{QUERY_STRING} ^q=([^&]+)&page=(.+)$ [NC]
RewriteRule ^all\.html$ /search.php?cid=&type=search&q=%1page=%2 [L]

推荐阅读