Home > Mobile >  Escape a # in rewrite rule in mod_rewrite
Escape a # in rewrite rule in mod_rewrite

Time:05-11

I'm getting a # in url when redirecting instead of the #. I tried escaping the hash with a \, but that didn't resolve. What am I missing?

Here's what I currently have:

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

CodePudding user response:

You need to add NE to the list of flags passed to RewriteRule to say No Escaping.

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#$1" [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, #, which will then result in a 404 Not Found error condition.

  • Related