• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Computing / htaccess Redirect with Hashtag

htaccess Redirect with Hashtag

Last updated on April 23, 2014 by Sal Ferrarello

I had to setup a redirect the other day with a hashtag. Whenever a visitor hit a specific URL, I wanted to redirect them to another URL but I also wanted to include a hashtag to indicate where they came from when tracking the information in Google Analytics.

It appears the secret is the NE flag otherwise the # gets escaped as %23.

Example of htaccess rule to Redirect with Hashtag

RewriteRule ^(?i:redirectme)$ http://example.com/common/#from-redirectme [NE,R=301,L]

http://example.com/redirectme
redirects to
http://example.com/common/#from-redirectme

photo credit: much0 via photopin cc

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit

Filed Under: Computing, Programming Tagged With: redirects

Reader Interactions

Comments

  1. Frank says

    December 12, 2014 at 4:35 am

    Thanks for the rewrite. What I’m looking for is a rewrite for a hashtag that has been moved:

    https:/www.mydomain.com/#/!/xyz.html to https://www.mydomain.com/#!/xyz

    There are two changes:
    /#/! has been replaced by /#!
    and the html of the hash has been removed.

    Thx for your feedback.

    Reply
    • Sal Ferrarello says

      December 16, 2014 at 10:10 pm

      Hi Frank,

      Unfortunately, you can not use an htaccess redirect based on the value after the `#`.

      The hashtag and anything after it are not sent to the server.

      You can read more about this here, http://stackoverflow.com/questions/8185188/redirect-url-with-hash-using-htaccess-file

      Reply
  2. David Ball says

    January 6, 2016 at 6:57 pm

    Thanks Sal, this is what I was looking for and helped me solve a problem!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in