.htaccess mod_rewrite GET形式的未知数量的变量 [英] .htaccess mod_rewrite Unknown number of Variables of a GET form

查看:54
本文介绍了.htaccess mod_rewrite GET形式的未知数量的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做什么:

我正在使用高级过滤器(很多复选框)进行搜索,并且使用了PHP的功能,即以[]结尾给所有这些过滤器取相同的名称.这是一个简化的示例:

I'm creating a search with advanced filters (lots of checkboxes) and I'm using that PHP feature of giving all of them same name with [] at end. Here is an simplified example:

(HTML)

<h3>Elements</h3>
<label><input type="checkbox" name="elementPicked[]" value="1" checked="checked" /> Fire</label>
<label><input type="checkbox" name="elementPicked[]" value="2" checked="checked" /> Water</label>
<label><input type="checkbox" name="elementPicked[]" value="3" checked="checked" /> Wind</label>

<h3>Rarity</h3>
<label><input type="checkbox" name="rarityPicked[]" value="10,11,12" /> Common</label>
<label><input type="checkbox" name="rarityPicked[]" value="19,20,21,22" /> Uncommon</label>
<label><input type="checkbox" name="rarityPicked[]" value="29,30,31,32" /> Rare</label>

<!-- (...other somethingPicked[] types, like atk, def and cost...) -->

(PHP)

if(!empty($_GET['elementPicked'])) {
    foreach($_GET['elementPicked'] as $elementPicked) {
        //do something with each $elementPicked
    }
}

对于mod_rewrite,当前我的重写工作如下:

For the mod_rewrite, currently my rewrite works as following:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#Example: ./
RewriteRule ^index$ index.php [NC,L]

#Example: ./p/dashboard
RewriteRule ^p/([^/]*)$ index.php?z=$1 [NC,L,QSA]

这意味着如果我呼叫http://example.com,它将重写为呼叫http://example.com/index.php.

This means that if i call http://example.com, it will rewrite to call http://example.com/index.php.

如果我呼叫http://example.com/p/cardlist,它将重写为呼叫http://example.com/index.php?z=cardlist. 这个很简单.

And if i call http://example.com/p/cardlist, it will rewrite to call http://example.com/index.php?z=cardlist. Pretty easy this one.

因此,现在出现htaccess问题:

对于此表单,URL如下所示: http://example.com/p/carddisplay?elementPicked%5B%5D=1&elementPicked%5B%5D=2&elementPicked%5B%5D=3&rarityType=Select&rarityPicked%5B%5D=39%2C40%2C41%2C42&rarityPicked%5B%5D=49%2C50%2C51%2C52 由于是搜索,因此人们以后可以使用相同的网址,因此POST不是一种选择(它不会为其他会话保存"所选的复选框).

For this form, the URL gets like this: http://example.com/p/carddisplay?elementPicked%5B%5D=1&elementPicked%5B%5D=2&elementPicked%5B%5D=3&rarityType=Select&rarityPicked%5B%5D=39%2C40%2C41%2C42&rarityPicked%5B%5D=49%2C50%2C51%2C52 Since is a search and people could come to the same url later, POST is not an option (it would not 'save' the chosen checkboxes for other session).

我想要实现的是http://example.com/p/carddisplay/var1/something/var2/something/var3/something...或翻译上面的示例http://example.com/p/carddisplay/elementPicked%5B%5D/1/elementPicked%5B%5D/2/elementPicked%5B%5D/3/rarityType/Select/rarityPicked%5B%5D/39%2C40%2C41%2C42/rarityPicked%5B%5D/49%2C50%2C51%2C52(或者如果可以删除url编码的[]也将是很棒的,因为它仍然很丑陋.)使它更漂亮的任何建议欢迎).

What I wanted to achieve was http://example.com/p/carddisplay/var1/something/var2/something/var3/something... or translating the above example, http://example.com/p/carddisplay/elementPicked%5B%5D/1/elementPicked%5B%5D/2/elementPicked%5B%5D/3/rarityType/Select/rarityPicked%5B%5D/39%2C40%2C41%2C42/rarityPicked%5B%5D/49%2C50%2C51%2C52 (or if can remove the url-encoded [] would be awesome too because is still ugly as hell.. Any suggestions for getting it prettier are welcome).

我在这里找到了想要的东西-> .htaccess网址中有多个变量的mod_rewrite

I found something INVERSE to what I wanted here -> .htaccess mod_rewrite with multiple variable in url

但是无论如何,我无法使其工作混乱,因为我有多个var[]. 实际上可以通过重写实现吗?

But anyways, I couldn't make it work messing with it, because I have more than one var[]. Is this actually possible to achieve with rewrite?

推荐答案

您可以在

You can capture the variables one by one in a RewriteCond and loop on that

RewriteEngine On

RewriteBase /

# capture query string variables
RewriteCond %{QUERY_STRING} ^(.+?)=(.+?)&(.+)$
RewriteRule ^/?p/carddisplay.*$ $0/%1/%2?%3 [L]

# capture last variable and redirect
RewriteCond %{QUERY_STRING} ^(.+?)=(.+?)$
RewriteRule ^/?p/carddisplay.*$ $0/%1/%2? [R,L]

# redirect URL with empty query string to index.php
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/?p/(.*)$ index.php?z=$1 [NC,L]

只要有查询字符串并建立新的URL,就会循环,然后将重定向发送给客户端.

This loops as long as there's a query string and builds up the new URL, then sends a redirect to the client.

如果要删除[]/%5B%5D,请将RewriteCond更改为类似

If you want to remove []/%5B%5D, change the RewriteCond to something like this

RewriteCond %{QUERY_STRING} ^(.+?)%5B%5D=(.+?)(?:&(.*))?$

这里会发生什么?

当请求/p/carddisplay?var1=value1&var2=value2来自表单时,首先将浏览器重定向到/p/carddisplay/var1/value1/var2/value2.当浏览器随后请求此URL时,它将被重写并在内部重定向到index.php?z=carddisplay/var1/value1/var2/value2.

What happens here?

When a request /p/carddisplay?var1=value1&var2=value2 comes from a form, the browser is first redirected to /p/carddisplay/var1/value1/var2/value2. When the browser then requests this URL, it is rewritten and internally redirected to index.php?z=carddisplay/var1/value1/var2/value2.

这仅用一个$_GET['z']参数调用PHP页面index.php,该参数的值为carddisplay/var1/value1/var2/value2.

This calls the PHP page index.php with just one $_GET['z'] parameter, which has the value carddisplay/var1/value1/var2/value2.

/p/...开头的任何其他请求都在内部重定向到index.php?z=...,而没有其余参数.

Any other request starting with /p/... is internally redirected to index.php?z=... without the remaining parameters.

这篇关于.htaccess mod_rewrite GET形式的未知数量的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆