Tuesday, 2 July 2019

Avoid encoding dispatcher URLs

Rewrite rules on Apache web server that attempt to redirect to URLs with special characters such as & or ? or #anchor aren't being redirected properly.  For example:
RewriteRule ^/we-retail.html /we-retail.html#anchor [R=301,L]

To avoid encoding the # character, add the NE flag to the rewrite rules:
RewriteRule ^/test.html /test.html#anchor [NE,R=301,L]

Special characters such as & and ?, 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.
Reference: RewriteRule Flags

No comments :

Post a comment