在 mod_rewrite 中的重写规则中转义 #

Escape a # in rewrite rule in mod_rewrite

我在重定向时在 url 中得到了 %23 而不是 #。我尝试使用 \ 转义散列,但没有解决。我错过了什么?

这是我目前拥有的:

# Redirect sub domain to external
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^(.*) https://www.example.come/path/with-a-hash/#/end-of-path/ [L,R]

您需要将 NE 添加到传递给 RewriteRule 的标志列表中,以表示不转义。

By default, special characters, such as & and ?, for example, will be converted to their hexcode equivalent. Using the [NE] flag prevents that from happening.

RewriteRule "^/anchor/(.+)" "/bigpage.html#" [NE,R]

The above example will redirect /anchor/xyz to /bigpage.html#xyz. Omitting the [NE] will result in the # being converted to its hexcode equivalent, %23, which will then result in a 404 Not Found error condition.