frshjb373
frshjb373

Reputation: 627

Escape a # in rewrite rule in mod_rewrite

I'm getting a %23 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]

Upvotes: 0

Views: 67

Answers (1)

Quentin
Quentin

Reputation: 943214

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, %23, which will then result in a 404 Not Found error condition.

Upvotes: 1

Related Questions