如何使用.htaccess规则删除百分比编码的斜杠?

Tza*_*man 6 apache .htaccess

我希望"捕获"进入我的服务器的传入URL,其URL中包含额外的"%252F"字符(由于某些历史原因),并将它们(301)重定向到没有这些字符的相同URL.例如:传入网址:www.sample.com/content/%252F/foo .htaccess规则:www.sample.com/content/foo

我必须能够在url(在同一个地方)中捕获这些字符的几个实例,例如:www.sample.com/content/%252F/%252F/%252F/foo并将它们全部删除.

我应该使用哪种.htaccess RewriteRule?

Tar*_*del 0

AFAIK RewriteRule 必须知道传入请求 URL 的精确格式。因此,不支持其中包含任意数量的 %252F 字符串。

不过,您可以做的是使用自定义 404 页面来解析 %252F 的请求 URL。自定义 404 页面将遵循以下逻辑:

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.
Run Code Online (Sandbox Code Playgroud)

这应该会给你你想要的效果。不用担心多余的斜杠,apache 会忽略它们。