301 Htaccess RewriteRule Query_String

冷暖自知 提交于 2019-12-23 16:23:55

问题


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/#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 :)!


回答1:


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.



来源:https://stackoverflow.com/questions/3817729/301-htaccess-rewriterule-query-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!