Save up to 25% on pro security plugins w/ our Security Bundle »
Web Dev + WordPress + Security

Redirecting Hash Fragments with .htaccess

During this year’s site redesigns, I noticed in the server logs some 404 errors for various WordPress comments. These 404 requests each involved a fragment identifier (i.e., character string beginning with a pound sign, #) being interpreted as its HTML entity hex equivalent, %23. It may not seem like a big deal, but these days every detail counts, so it’s wise to clean up as many 404 errors as possible. Thus, here is a simple .htaccess technique for redirecting hash-fragment requests to their proper destination.

WordPress Example

The logs were showing numerous requests where the hash symbol # was replaced with its HTML character code, %23. These were interpreted literally, such that the requested URLs were recorded as such:

https://example.com/some-path/%23comment-61814

To fix this with .htaccess, we can do this:

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

Here we are rewriting the request such that the correct path and comment number are specified in the redirect URL. So the first (.*) is used to replace the $1, and the second (.*) is used to replace the $2. The key here is the NE flag, which prevents # from being converted to its hex code equivalent %23 during the rewrite.

General Example

This technique can be applied to the general case. Let’s say you want to redirect /example/%23identifier to /example/#identifier. It’s as easy as adding this to your .htaccess file:

RewriteRule ^/example/%23identifier /example/#identifier [R=301,L,NE]

Or let’s say that we want to redirect such that a part of the URL is appended as a hash fragment:

RewriteRule ^/some-path/(.*)/?$ /some-path/#$1 [R=301,L,NE]

So for example, if the request is for /some-path/whatever/, it will be redirected to /some-path/#whatever.

Again, the NE flag is instrumental to making this happen.

Jeff Starr
About the Author
Jeff Starr = Web Developer. Security Specialist. WordPress Buff.
Digging Into WordPress: Take your WordPress skills to the next level.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
Banhammer: Protect your WordPress site against threats.
Thoughts
Fall season almost here :)
My greatest skill on social media is the ability to simply ignore 98% and keep scrolling without interacting.
Enjoying this summer, getting some great positive energy. Refreshing and inspiring.
☀️ Pro plugin giveaway! Enter to win 1 of 4 lifetime licenses for our WordPress security plugins, including 10-site Security Bundle!
There is no end to what humans can achieve when they work together.
Excellent (and free) tool to test your site's SSL configuration.
Plugin updates! All of our free and pro plugins ready for WordPress 6.2.
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.