开发者

How can I remove percent encoded slashes using .htaccess rule?

I want to "catch" incoming URLs that enter my server with extra "%252F" characters in the URL (due to some historical reasons) and redirect them (301) to the same url without these characters开发者_JAVA百科. For example: incoming url: www.sample.com/content/%252F/foo after .htaccess rule: www.sample.com/content/foo

I have to be able to catch several instances of these characters in the url (in the same place), like : www.sample.com/content/%252F/%252F/%252F/foo and remove them all.

Which .htaccess RewriteRule should I use?


AFAIK RewriteRule must know the precise format of the incoming request URL. Therefore, having an arbitrary number of %252F strings in there isn't supported.

What you can do though is have a custom 404 page that parses the request URL for %252F. The custom 404 page would follow the following logic:

Search the request URL for %252F
  If found, replace it with a / (or nothing, up to you) and return a 301 header
    and an appropriate error page.
  Else return a 404 header and appropriate error page.

That should give you the effect you want. Don't worry about the extra slashes, apache ignores them.


RewriteRule (/.*?)?((/%2F)+)(.*) $1$4 [redirect=301,last]


This works

RewriteEngine on
RewriteBase /
RewriteRule ^(.*)/%2F(.*) /$1$2 [R=301,L]

but there is a drawback, it does multiple redirects ( 1 for each group of %252F )

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜