rewrite and url decoding

霸气de小男生 提交于 2019-12-21 20:57:44

问题


It seems a site is linking to mine in a bad way. From google webmaster tools I see some 404 errors

domain.com/file.php?id=1 (404) Not found    (Date)

This url works ok but because of browser page decoding, the real (404) url is

domain.com/file.php%3Fid%3D1 (this is what my browser displays in the url input when I click on google url)

My first try was

RewriteRule ^(.*)\%3F(.*)$ $1?$2 [R=301, L]

to change %3F to '?' but it does not work. It is confusing what is real and what is en/decoded.

Thanks


回答1:


Hmm, try this:

RewriteRule ^(.*\%3F.*)\%26([^%]*)(\%3D)?(.*)$ $1&$2=$4 [N]
RewriteRule ^(.*)\%3F([^%]*)(\%3D)?(.*)$ $1?$2=$4 [R=301,L]

EDIT: I see you already tested for %3F... if that didn`t work, then something else might be up.




回答2:


It is not an elegant solution, but after months, it is the only way I found.

Regular case

domain.tld/dir/file.php?id=UID&qty=NUM&...   (from old site structure)

is rewritten as

domain.tld/newfile.php?id=UID&qty=NUM&... [R=301,L]

So, for requests like

domain.tld/dir/file.php%3Fid%3DUID%26qty=NUM%26...   (from old site structure)

I perform

RewriteRule ^dir/file\.php(.*)$ script.php?qs=$1 [R,L]

This MUST be AFTER all directives that involves dir/file.php

Then I can handle the query string $_GET['qs'] in script.php

It may not apply to other situations that would enter into a loop or undesired results.




回答3:


use [NE] flag in your RewriteRule



来源:https://stackoverflow.com/questions/8464298/rewrite-and-url-decoding

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