I have a single page website that uses the hash (#) to access different pages.
The problem is that some PDF viewers break my links encoding the hash character, replacing # with %23, so for example the link my.app.com/#product/22 becomes my.app.com/%23product/22.
I'm trying to correct this using an htaccess rule that rewrites the URL:

RewriteEngine On
RewriteRule ^(.*)%23(.*)$ /$1#$2 [L,R=301,NE]

but it doesn't seem to work, what can I do?


The RewriteRule pattern matches against the %-decoded URL-path. So, if the requested URL contains %23 (a URL encoded #) then you need to match a literal # in the RewriteRule pattern. Otherwise, your redirect seems to be OK.

For example:

RewriteRule ^(.*)#(.*)$ /$1#$2 [L,R=301,NE]

The NE (noescape) flag is required here in order to prevent the # in the substitution being URL encoded in the redirect response.

Clear your browser cache before testing. And test with 302 (temporary) redirects to avoid caching issues.

  • 1
    Thanks! The problem was exactly that the pattern needed to match the decoded %23 character, so ^(.*)#(.*)$! – Andrea Gobetti Oct 14 at 8:37

The # and everything after it should never get sent with the request.

That's just for linking to named a tags

  • 1
    That's true when the separator is a real # character, but when the character is encoded the entire URL including what is after the encoded # (%23) is sent to the server – Andrea Gobetti Oct 14 at 8:34
  • Right, which is why you shouldn't need to worry about real # in your rules. – pguardiario Oct 14 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.