重写动态 URL [英] Rewriting dynamic URLs

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

问题描述

我尝试将 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]

不过第一个比较清楚,不是处处都用category这个词.如果您想添加小写转换,我们甚至可以这样做:

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 None 并移动所有这些VirtualHost 中的东西.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 尝试处理请求的文件名.所以我首先检查我们是否在目标文件名上:

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]

然后我们将处理查询字符串(问号之后的所有内容).我们本来可以写得更简单的:

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]

在可用于 RewriteRule 的变量 %1 中捕获 ?category= 之后的所有内容.但是 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 解码
  • category 可能不是参数列表中的第一个参数
  • 它甚至可能不是最后一个 (&foo=bar)
  • 你之前可以有一些空格

已编辑:

我的第一个回答是:

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

使用 [^|&|%26|%20] 捕捉到它可能是起始参数或 & 之后的参数的事实.或空格,%26 是 &网址编码.

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 评论,我检查了更多,[category] ​​匹配 'category' 但也匹配 '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]

匹配'category' 参数,删除^ 允许将此参数放置在任何地方.但这也将匹配名为 subcategory 的参数.所以第一个字母必须是空格或 &(或查询字符串的开头).

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] 的原因,对我来说意味着链 OR &或空间或&在 urlencoded 中.但这也是错误的[] 中的 ^ 表示否定.所以我们需要在这里使用匹配括号:(^|&|%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

然后我们捕捉(类别)词,准确地说,我们实际上应该逐个字母地捕捉大写、小写和 urlencoded 版本的小写和大写字母,就像一个可怕的 [c|C|%43|%63][a|A|%61|%41][t|T|%74|...] 待续 -- 我可能应该使用括号而不是方括号,见鬼,检查此处的urlencoded字符列表.

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) 是 '=' 字符 urlencoded 与否.我本可以使用 [=|%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 '&'.

在这些条件之后,我们应用 RewriteRule.我们获取所有内容(这是 .*)并将其重定向到 /catalogsearch/result/?q=%2 我们使用 %3 和不是 $1$3 因为我们没有在 rewriteRule 上捕获任何内容,而是在之前的条件下(%3 是最后一个捕获的第三个数据条件所以它是 ([^&]+)).如果您在 rewriteRule 中添加 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

如果我玩一点网址编码:

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

但这不是游戏的结束,我会让你处理所有你可能想要检测 Foo 值的所有变化并将它们与 tolower 混合的 urlencoding 问题(这里它坏了).您甚至可能不会在参数或额外属性中遇到编码 url 的这些问题,但这很有趣 :-)

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 :-)

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

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