开发者

301 Htaccess RewriteRule Query_String

Problem: Visitors open the url website.com/?i=133r534|213213|12312312 but this url isn't valid anymore and they need to be forwarded to website.com/开发者_Python百科#Videos:133r534|213213|12312312

What I've tried: During the last hours I tried many mod_rewrite (.htaccess) rules with using Query_String, all failed. The last message in this topic shows a solution for this problem, but what would be the rule in my situation.

I'm very curious how you would solve this problem :)!


The following will handle the simple case you show. You'll need to add additional logic if you need to allow for other parameters in the query string or file names before the ?.

RewriteEngine On
RewriteCond %{QUERY_STRING} ^i=(.*)
RewriteRule ^.*  /#Video:%1? [NE,R=permanent]

Why is this tricky?

  • RewriteRule doesn't look at the query string, so you have to use RewriteCond to evaluate the QUERY_STRING variable and capture the part you'll need later (referenced via %1)
  • the hash character (#) is normally escaped, you must specify the [NE] flag
  • The trailing ? on the substitution string is required to suppress the original query string

I tested this on Apache 2.2.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜