简体   繁体   English

htaccess如何在url重写后访问/获取搜索参数

[英]htaccess how to access/get search parameters after a url rewrite

I have a htaccess rewrite rule, based upon a form being submitted. 我有一个htaccess重写规则,基于提交的表单。 I am trying to read the search parameters, this is a simple example of keyword search, but ideally more parameters eventually will be added: 我正在尝试阅读搜索参数,这是关键字搜索的一个简单示例,但理想情况下最终会添加更多参数:

single parameter url: //thesite.com/search?keyword=sample+text 单个参数url: //thesite.com/search?keyword=sample+text

2 parameter url: //thesite.com/search?keyword=sample+text&location=myCity 2参数url: //thesite.com/search?keyword=sample+text&location=myCity

In my htacess I have a simple Rewrite rule looking for the search like so: 在我的htacess中,我有一个简单的重写规则寻找搜索,如下所示:

RewriteRule ^.*search?(.*)$ /displayPage.php?page=search&options=$1

I access the options parameter like so: 我像这样访问options参数:

$searchOptions = filter_input(INPUT_GET, "options")!==NULL?filter_input(INPUT_GET, "options"):'';

when I output this variable it's has nothing. 当我输出这个变量时,它什么都没有。

I tried var_dump($_GET) and the result was: 我尝试了var_dump($_GET) ,结果是:

array (size=2)
  'page' => string 'search' (length=6)
  'options' => string '' (length=0)

The html used to generate the url: 用于生成url的html:

<form class="start-search" action="/search" method="get">
  <input id='keyword' name='keyword' class="search-str" type="text" value="" placeholder="Start Your Search">
  <!-- Meant to hold value for category -->
  <input class="search-cat" type="hidden" value="" placeholder="Start Your Search">

  <button class="btn" type="submit">
    <i class="fa fa-arrow-circle-o-right"></i>
  </button>
</form>

I was expecting the $searchOptions to contain the following: 我期待$ searchOptions包含以下内容:

for single parameter: keyword=sample+text 对于单个参数: keyword=sample+text

for more than one parameter: keyword=sample+text&location=myCity 对于多个参数: keyword=sample+text&location=myCity

I could than split the string based upon & and = and replace + with a whitespace. 我可以根据&和=拆分字符串,并用空格替换+。

Any ideas why the the parameters are not being included? 任何想法为什么没有包括参数?

Thank you so very much 非常感谢你

UPDATE: Applying Case 1 更新:应用案例1

RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^search?(.*)/$ displayPage.php?page=search&%1

My apache log reports the following: 我的apache日志报告如下:

"GET /search?keyword=test+text HTTP/1.1" 404 286

What is incorrect? 什么是不正确的? I tried removing the ? 我试过删除? from the rewrite rule, I replaced it with a / as the sample provided. 从重写规则,我用/作为提供的样本替换它。 I modified the html to include a / after it. 我修改了html以包含/之后。 When I did the following was reported in apache logs: 当我这样做时,在apache日志中报告了以下内容:

"GET /search/?keyword=test+text HTTP/1.1" 301 329
"GET /search?keyword=test+text HTTP/1.1" 404 286

ADDITIONAL HTACCESS INFO 额外的HTACCESS信息

I have the following already applied 我已经应用了以下内容

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

I have a bunch of other RewriteRule for other urls that are all functional. 我有一堆其他的RewriteRule用于其他功能齐全的网址。 This is my first query string one which has me stumped. 这是我的第一个查询字符串,它让我难倒。

Update2 UPDATE2

Trying Steve E. [QSA] 尝试Steve E. [QSA]

RewriteRule ^.*search?(.*)/$ displayPage.php?page=search&%1 [QSA]

Apache log reports Apache日志报告

 "GET /search?keyword=sample+search HTTP/1.1" 404 286

Solution: as provided by Steve E with one fix from above, forgot the / before the call to displayPage.php. 解决方案:由Steve E提供上面的一个修复程序,在调用displayPage.php之前忘记了/。 It works for single and multiple parameters. 它适用于单个和多个参数。 :) :)

RewriteRule ^.*search?(.*)/$ /displayPage.php?page=search&%1 [QSA]

Use the [QSA] flag on your RewriteRule 使用RewriteRule上的[QSA]标志

RewriteRule ^search?(.*)/$ displayPage.php?page=search&%1 [QSA]

Here is the docs 这是文档

Modifying the Query String By default, the query string is passed through unchanged. 修改查询字符串默认情况下,查询字符串不会更改。 You can, however, create URLs in the substitution string containing a query string part. 但是,您可以在包含查询字符串部分的替换字符串中创建URL。 Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. 只需在替换字符串中使用问号,即表示应将以下文本重新注入查询字符串。 When you want to erase an existing query string, end the substitution string with just a question mark. 如果要删除现有查询字符串,请仅使用问号结束替换字符串。 To combine new and old query strings, use the [QSA] flag. 要组合新旧查询字符串,请使用[QSA]标志。

Use RewriteCond We were used to use RewriteRule to match a URL, and then rewrite it. 使用RewriteCond我们习惯使用RewriteRule来匹配一个URL,然后重写它。 However, RewriteRule will always ignore your query string (GET data) so we need to use RewriteCond to capture the values in the query string. 但是,RewriteRule将始终忽略您的查询字符串(GET数据),因此我们需要使用RewriteCond来捕获查询字符串中的值。

This is the syntax: 这是语法:

RewriteCond %{QUERY_STRING} ^(.*)$
  • %{QUERY_STRING} captures the matched query string into % variable. %{QUERY_STRING}将匹配的查询字符串捕获到%变量中。
  • %1 will carry the matched content of the first group of brackets. %1将携带第一组括号的匹配内容。 The second part ^(.*)$ is the pattern you want to match. 第二部分^(.*)$是您要匹配的模式。
  • ^ means “start with” ^表示“以...开头”
  • $ means “end” $表示“结束”
  • Like regular expressions, the dot in (.*) matches any characters. 与正则表达式一样, (.*)中的点匹配任何字符。
  • The asterisk * inside the brackets is a quantifier that match 0 occurrence to infinite occurrences. 括号内的星号*是一个量子,它匹配0次出现到无限次出现。

    Therefore, combining them, (.*) matches any characters So, clearly, in this example, it matches the whole query string part. 因此,组合它们, (.*)匹配任何字符。因此,显然,在此示例中,它匹配整个查询字符串部分。 Some common ways of capturing query strings 捕获查询字符串的一些常用方法

Case 1 情况1

This case is for you if you've rewritten your URL this way, for example: FROM site.com/show.php?id=seo TO site.com/show/seo/ 如果您以这种方式重写了URL,则适用于此案例,例如:FROM site.com/show.php?id=seo to site.com/show/seo/

And you WANT to pass or append some GET data using the rewritten URL, like: site.com/show/seo/?lang=en&source=firefox you want it to append like site.com/show.php?id=seo&lang=en&source=firefox 并且您希望使用重写的URL传递或附加一些GET数据,例如: site.com/show/seo/?lang=en&source=firefox您希望它附加到site.com/show.php?id=seo&lang=en&source=firefox

This is the htaccess rules: 这是htaccess规则:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^show/(.*)/$ show.php?id=$1&%1

This RewriteCond captures whole query string (first bracket) into %1 In this case, the captured string %1 is “lang=en&source=firefox” We then do our normal URL rewriting and append the %1 to the URL after a ampersand ( & ). 这个RewriteCond将整个查询字符串(第一个括号)捕获到%1在这种情况下,捕获的字符串%1是“lang = en&source = firefox”然后我们进行正常的URL重写并在符号后附加%1到URL( & )。

Case 2 案例2

This case is for you if you know specifically or want to control what query string you are going to use. 如果您具体了解或想要控制要使用的查询字符串,则此情况适合您。

For example, you only want to match lang=xxx and NOTHING ELSE , for example: site.com/show/seo/?lang=en&more=1 you want it to append like site.com/show.php?id=seo&lang=en , ignoring other unknown query string. 例如,您只想匹配lang=xxxNOTHING ELSE ,例如: site.com/show/seo/?lang=en&more=1 ? lang=xxx & more lang=xxx您希望它附加到site.com/show.php?id=seo&lang=en ,忽略其他未知查询字符串。

RewriteEngine On
RewriteCond %{QUERY_STRING} ^lang=(\w+)$
RewriteRule ^show/(.*)/$ show.php?id=$1&lang=%1
  • This RewriteCond captures lang's GET data (first bracket) into %1 , 这个RewriteCond将lang的GET数据(第一个括号)捕获到%1
  • \\w matches word characters \\w匹配单词字符
  • The plus + means one occurrence or more +表示一个发生或多个
  • In this case, the captured string %1 is “en” 在这种情况下,捕获的字符串%1是“en”

Case 3 案例3

Similar as Case 2, but this case we have more queries to match. 与案例2类似,但是这种情况下我们有更多的查询要匹配。

For example, you only want to match lang=xxx and time=000 , for example: site.com/show/seo/?lang=en&time=100 you want it to append like site.com/show.php?id=seo&lang=en&time=100 例如,您只想匹配lang=xxxtime=000 ,例如: site.com/show/seo/?lang=en&time=100 ? lang=xxx & time=000您希望它附加到site.com/show.php?id=seo&lang=en&time=100

RewriteEngine On
RewriteCond %{QUERY_STRING} ^lang=(\w+)&time=(\d+)$
RewriteRule ^show/(.*)/$ show.php?id=$1&lang=%1&time=%2
  • This RewriteCond captures lang's GET data (first bracket) into %1 这个RewriteCond将lang的GET数据(第一个括号)捕获到%1
  • This RewriteCond also captures time's GET data (second bracket) into %2 此RewriteCond还将时间的GET数据(第二个括号)捕获到%2
  • \\w matches word characters \\w匹配单词字符
  • \\d matches decimals \\d匹配小数
  • The plus + means one occurrence or more +表示一个发生或多个

In this case, the captured string %1 is “en” , %2 is "100". 在这种情况下,捕获的字符串%1是“en”, %2是“100”。

Useful Link to Apache mod rewrite Apache mod重写的有用链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM