与在.htaccess中选择的查询字符串参数重写URL重写、字符串、参数、htaccess

2023-09-02 00:27:28 作者:余生依恋你

最近,我改变了我的CMS,并要重写我的一些网址,以配合新的URL /查询字符串参数格式。

I recently changed my CMS, and want to rewrite some of my URLs to match the new URL/query string parameter format.

旧网址是:

http://www.mysite.com/search.cgi?tag=foo&blog_id=bar&other=baz

新的URL应该是:

The new URL should be:

http://www.mysite.com/?s=foo

在换句话说,有旧格式的几个查询字符串参数,但我只在乎重写标记参数为取值,同时保持相同的值。其它参数应被丢弃。当然这些参数的顺序并不重要。它也应该没有关系标签是否是唯一的参数或不

In other words, there were several query string parameters in the old format, but I only care to rewrite the tag param to s while keeping the same value. The other parameters should be discarded. Of course the order of the parameters shouldn't matter. It also shouldn't matter whether tag is the only parameter or not.

任何想法?

推荐答案

您可以尝试使用常规的前pression和分组,如果你的服务器支持。如果我没有记错,你必须重写只有一个参数,我想你可以(如果你使用的是Apache与mod_rewrite的)尝试类似:

You can try to use regular expression and grouping, if your server supports that. If I am not mistaken you have to rewrite only one parameter, I guess you could try something like (if you are using apache with mod_rewrite):


RewriteCond %{QUERY_STRING} ^.*(btagb=(w+|&w+;)+)
RewriteRule ^(.+) /$1?%1 [L]

编辑:我改进了常规EX pression一点点地匹配标签,无论它的位置在查询字符串和preserve特殊字符序列,如&放大器;放大器; 另外也应避免与类似的参数(即:它不会被称为alttag参数相匹配)匹配

I improved the regular expression a little bit to match "tag" regardless of its position in the query string and to preserve special characters sequences such as & In addition it should avoid matches with similar parameters (i.e.: it wouldn't match a parameter called "alttag").

编辑#2:的替代(尤其是如果你要筛选一些参数)是使用外部程序做了改写。本页面:http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritemap (特别是部分外部重写程序)中包含的有用信息。

Edit #2: An alternative (especially if you have to filter several parameters) is to use an external program to do the rewrite. This page: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritemap (in particular the section "External rewriting program") contains useful information.

 
精彩推荐
图片推荐