重写动态网址 [英] Rewriting dynamic URLs

查看:346
本文介绍了重写动态网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重写1的动态URL在动态URL如下:

I' trying to rewrite 1 dynamic URL to another dynamic URL as follows;

/category?category=News to
/categorysearch/result/?q=news

我想知道是否有人对如何能在htaccess的做一个想法?

I was wondering if anyone had an idea on how this can be done in htaccess?

感谢。

推荐答案

因此​​,最终的 EDITED 解决方案(你链接的.htaccess):

So the final EDITED solution is (for your linked .htaccess):

RewriteCond %{REQUEST_FILENAME} ethical [NC]
RewriteCond %{QUERY_STRING} (^|&|%26|%20)ethical(=|%3D)([^&]+) [NC]
RewriteRule .* /catalogsearch/result/?q=%3 [L,R]

如果我在当前的问题,使用类别项,如:

If I use the 'category' terms like in this current question:

RewriteCond %{REQUEST_FILENAME} category [NC]
RewriteCond %{QUERY_STRING} (^|&|%26|%20)category(=|%3D)([^&]+) [NC]
RewriteRule .* /categorysearch/result/?q=%3 [L,R]

但第一个更清晰,没有任何地方用这个词类别。如果您要添加的小写转换,我们甚至会做的:

But the first one is more clear, the word category is not used everywhere. If you want to add the lowercase transformation we'll even do:

# outside of a Directory section
RewriteMap lowercase int:tolower
# in a Directory section (not a .htaccess)
RewriteCond %{REQUEST_FILENAME} category [NC]
RewriteCond %{QUERY_STRING} (^|&|%26|%20)category(=|%3D)([^&]+) [NC]
RewriteRule .* /catalogsearch/result/?q=${lowercase:%3} [L,R]

但正如评论这最后一个与TOLOWER不能工作在的.htaccess - 反正的.htaccess是坏的,真的,演出,你应该认真考虑设置了AllowOverride无移动这一切东西在一个虚拟主机。我也很高兴的事情,如 rewriteMaps 不能在.htaccess文件的工作。

But as commented this last one with tolower could not work in a .htaccess -- anyway .htaccess are bad, really, for performances, you should really think about setting AllowOverride None and move all this stuff in a VirtualHost. And nice things such as rewriteMaps cannot work on .htaccess files.

所以,现在我的解释的规则。首先,在您的情况,主要问题是,一切都在 QUERY_STRING ,不是要求文件名。而大多数的RewriteRules尝试工作,对请求的文件名。所以,我先检查一下我们的目标quested文件名:

So Now I'll explain the rules. First the main problem in your situation is that everything after the "?" is the query_string, not the requested filename. And most rewriteRules try to work on the requested filename. So I first check we're on the targeted quested filename:

RewriteCond %{REQUEST_FILENAME} category [NC]

那么,我们要工作,对查询字符串(问号后面的所有内容)。我们可以写的东西很simplier这样的:

Then we are going to work on the Query string (everything after the question mark). We could have written something quite simplier like that:

RewriteCond %{REQUEST_FILENAME} category [NC]
RewriteCond %{QUERY_STRING} ^category=(.+) [NC]
RewriteRule .* /catalogsearch/result/?q=${lowercase:%1} [L,R]

要经过?类别捕获一切=一个变量1%可用于重写规则。但QUERY_STRING参数有几个问题,让我们试着有一些乐趣的:

To catch everything after ?category= in a variable %1 available for the RewriteRule. But the QUERY_STRING parameter has several problems, let's try to have some fun with it:

  • 在URL解码尚未对这个参数
  • 类是也许不是在参数列表
  • 第一个参数
  • 这甚至不是最后一个(安培;富=酒吧)
  • 您可能之前有一些空格

EDITED

我的第一个答案是:

RewriteCond %{REQUEST_FILENAME} category [NC]
RewriteCond %{QUERY_STRING} [^|&|%26|%20][category](=|%3D)([^&]+) [NC]
RewriteRule .* /catalogsearch/result/?q=${lowercase:%2} [L,R]

使用 [^ |&放大器; | 26%| 20%] 捕捉事实上,它可能是开始参数或后一个参数的&放大器;或空格, 26%是一个与放大器; urlen codeD。

With [^|&|%26|%20] catching the fact it could be the starting parameter or a parameter after an & or a space, %26 is an & urlencoded.

但是,这是错误,感谢@mootinator我查了一点评论,【类别】是匹配的类,但也atcatcategory和论文字母的任意组合。

But this was wrong, thanks to @mootinator comment I've checked a little more, [category] is matching 'category' but also 'atcatcategory' and any combination of theses letters.

RewriteCond %{QUERY_STRING} category(=|%3D)([^&]+) [NC]

时的相匹配的类别的说法,去除 ^ 允许这种说法被放置在任何地方。但是,这也将命名为子类参数相匹配。因此,第一个字母必须是一个空格或放大器; (或查询字符串的开头)。

Is matching the 'category' argument, removing the ^ allows this argument to be placed anywhere. But this would also match an argument named subcategory. So the 1st letter must be a space or & (or the start of the query string).

这是对的原因[^ |&放大器; | 26%| 20%] ,这意味着对我来说链或放的开始;或空间或放大器;在urlen codeD。但是,这也是错误 ^ [] 表示否定。所以我们需要在这里使用以及括号匹配:(^ |&放大器; |%26 |%20),现在它的工作原理,唯一的问题是,值了理由是现在的3%,而不是2%

This was the reason for [^|&|%26|%20], meaning for me start of chain OR & or space or & in urlencoded. But that was also wrong, the ^ inside [] means negate. So we need to use here as well matching parenthesis: (^|&|%26|%20), now it works, the only problem is that the value of the argument is now %3 and not %2

然后我们赶上(类别)的话,要真正准确的,我们其实应该抓住它字母大写,小写和urlen codeD信的版本之后字母的大小写,像一个awfull [C | C | 43%| 63%] [A | A | 61%| 41%] [T | T | 74%| ...]待续 - - 我也许应该使用,而不是括号括号,地狱,在这里检查 urlen codeD人物列表

Then we catch the (category) word, to be really exact we should in fact catch it letter after letter with uppercase, lowercase, and urlencoded version of the letter in lower and uppercase, something like an awfull [c|C|%43|%63][a|A|%61|%41][t|T|%74|...]to be continued -- and I should maybe use parenthesis instead of brackets, hell, check urlencoded characters list here.

(= |%3D)是'='字符urlen codeD或没有。 <击>我可以使用 [= |%3D] %3D 是不是在这种情况下很好地匹配,我不明白为什么(mod_rewrite的是奇怪的事情土地)。因为用这个第一个匹配的括号,我们将不得不使用%2变量,而不是%1。

(=|%3D) is the '=' character urlencoded or not. I could have used [=|%3D] but the %3D is not matched well in this case, I don't understand why (mod_rewrite is a land of strange things). Because of this first matching parenthesis used we'll have to use %2 variable and not %1.

然后,我们有很好的([^&功放;] +)这意味着什么,但不是'和;

And then we have the nice ([^&]+) which means anything but not an '&'.

在论文的条件下,我们应用重写规则。我们把所有的东西(这是。* ),并将其重定向到 / catalogsearch /结果/ Q =%2 其中,我们使用%3 ,而不是 $ 1 $ 3 ,因为我们不要在重写规则,但之前(%3 是在最后一个条件的第三捕获数据,所以它的([^&安培的条件获取任何东西; ] +))。 如果在重写规则添加 QSA 标志([L,R,QSA]),你甚至会找回其他的参数对最终的查询字符串,以便:

After theses conditions we apply the RewriteRule. We take everything (this is .*) and redirect it to /catalogsearch/result/?q=%2 where we use %3 and not $1 or $3 as we do not capture anything on the rewriteRule but on a condition before (%3 is the third captured data on the last condition so it's ([^&]+)). If you add the QSA flag ([L,R,QSA]) in the rewriteRule you will even get back other parameters on the final query string so that:

ethical?bar=toto& ethical=Foo&zorglub=titi 
=> is redirected to 
catalogsearch/result/?q=foo&bar=toto&%2520ethical=Foo&zorglub=titi

如果我有一点点的URL编码玩:

And if I play with a little url encoding:

ethical?bar=toto%26 ethical%3DFoo&zorglub=titi 
=> is redirected to 
catalogsearch/result/?q=foo&bar=toto%2526%2520ethical%253DFoo&zorglub=titi

但是,这不是戏结束时,我就让你处理所有你要检测的富价值的所有变化,并与TOLOWER混合他们(这里不对头)的URL编码的问题,你可以拥有它。您将可能甚至没有连接codeD网址在你的参数或附加属性的论文的问题,但是这很有趣: - )

But this is not the end of the play, I'll let you handle all the urlencoding problems you could have it you want to detect all variations on Foo value and mix them with the tolower (here it's broken). You will maybe not even have theses problems of encoded url in your parameters or extra attributes, but that was fun :-)

这篇关于重写动态网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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