Forum Moderators: phranque

Message Too Old, No Replies

Special characters in htaccess

Special characters in htaccess

         

Awarn

7:32 pm on May 15, 2015 (gmt 0)

10+ Year Member



I would like to use a # sign in a htaccess rewite. For example I would like to do the following:
RewriteRule ^test\.html$ http://www.example.com/#test [R=301,L] but the # sign is not recognized properly. Does anyone know how to fix this?

[edited by: phranque at 8:40 pm (utc) on May 15, 2015]
[edit reason] exemplified domain [/edit]

whitespace

8:27 pm on May 15, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



In order to use special characters (such as #) in the RewriteRule substitution you need to use the NE (noescape) flag to prevent the the special character from being percent encoded.


RewriteRule ^test\.html$ http://example.com#test [R=301,NE,L]

lucy24

8:40 pm on May 15, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I would like to use a # sign in a htaccess rewrite.

Yes, that's straightforward. You need to use the [NE] flag in your RewriteRule. In fact this is the exact situation mentioned in the docs [httpd.apache.org] (no change between 2.2 and 2.4) for this flag. "No Escape" = send the literal # back to the browser, instead of converting it to %23.

Caution! The # can only be used in a target, not in a pattern.

All this is assuming that you mean # in the normal sense, as an in-page fragment identifier. If you're using # in filenames you are probably SOL.

Edit: Oh, good, phranque stopped by while I was typing. I was about to wander off looking for an auto-link repair :)

Awarn

8:44 pm on May 15, 2015 (gmt 0)

10+ Year Member



Thank you very much.