现代重写:处理两个GET变量 [英] Mod Rewrite: Handling two Get Variables

查看:129
本文介绍了现代重写:处理两个GET变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么可以编辑该URL重写,包括进一步得到变量附加在网址是什么?我已经尝试了一些attemps但我总是得到一个500内部服务器错误!

How can I edit this url rewrite to include a further get variable being appended to the URL? I have tried a few attemps but I always get a 500 internal server error!

我添加的意见,看看我试图达到的。

I have added comments to show what I am trying to achieve.

# /view.php?user=h5k6&page=1 externally to /h5k6/1
# Some times the page get variable will not exist so it should just show /h5k6
RewriteCond %{THE_REQUEST} ^GET\ /view\.php
RewriteCond %{QUERY_STRING} ^([^&]*&)*user=([^&]+)&?.*$
RewriteRule ^view\.php$ /%2? [L,R=301]

# /h5k6/1 internally to /view.php?t=h5k6&page=1
RewriteRule ^([0-9a-z]+)$ view.php?t=$1 [L]

感谢所有的帮助

推荐答案

与URI查询参数的问题是,它们可能出现在任何顺序。有两个参数你刚才有两个排列( A之前乙的和的 A之前乙的)。但是,更多的参数要提取您有( n个多排列的!)。

The problem with URI query arguments is that they may appear in any order. With two arguments you just have two permutations (A before B and B before A). But the more arguments you want to extract the more permutations you have (n!).

这意味着你应该把论点正确的顺序,以便您可以将它们在同一时间:

That means you should put the arguments in the right order so that you can them at a time:

RewriteCond %{THE_REQUEST} ^GET\ /view\.php
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)(page=[^&]+)&?([^&].*)?$
RewriteCond %3&%1%4 ^(([^&]*&)*)(user=[^&]+)&?([^&].*)?$
RewriteCond %3&%1%4 ^user=([^&]+)&page=([^&]+)&?([^&].*)?$
RewriteRule ^view\.php$ /%1/%2?%3 [L,R=301]

第一个条件检查请求行就像在你的规则。第二个条件挑选出的的参数,并把它在查询字符串的开始。第三个条件确实用的用户的参数相同,这样的顺序一直是:先的用户的,第二的的。然后,我们用第四个条件同时采取这两个参数值在重写规则替换使用它们。查询的其余部分被追加到URI更换(见 3%),但你可以留下了,如果你想要的。

The first condition checks the request line like in your rule. The second condition picks the page argument and puts it at the start of the query string. The third condition does the same with the user argument so that the order always is: first user, second page. Then we use the fourth condition to take both argument values at a time to use them in the RewriteRule replacement. The rest of the query is appended to the replacement URI (see %3) but you can leave that off if you want.

正如你所见,你要提取更多的参数,更复杂的它得到。而且很容易做到这一点与更强大不是像PHP的mod_rewrite的语言。

As you see, the more arguments you want to extract, the more complex it gets. And it is easier to do that with a language that is more powerful than mod_rewrite like PHP.

这篇关于现代重写:处理两个GET变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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