Anchor instead of comment in htaccess

Is there a way in an htaccess file to interpret the # literally, and not as a comment?

For example I'm using mod_rewrite and I'd like to take all incoming links to a particular folder, and redirect them to a certain page with an anchor at the end.

Something like this:

RewriteRule ^blogposts/([^/\.]+)/?$ blogposts/index.php?title=$1#$1
LVL 5
bswinnertonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

miahiCommented:
Try replacing the "#" sign with "\%23" in that link: blogposts/index.php?title=$1\%23$1
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
miahiCommented:
You also need the [NE] flag set:  blogposts/index.php?title=$1\%23$1 [NE]

'noescape|NE' (no URI escaping of output)
    This flag prevents mod_rewrite from applying the usual URI escaping rules to the result of a rewrite. Ordinarily, special characters (such as '%', '$', ';', and so on) will be escaped into their hexcode equivalents ('%25', '%24', and '%3B', respectively); this flag prevents this from happening. This allows percent symbols to appear in the output, as in

    RewriteRule /foo/(.*) /bar?arg=P1\%3d$1 [R,NE]
    which would turn '/foo/zed' into a safe request for '/bar?arg=P1=zed'.
0
bswinnertonAuthor Commented:
Hmm, I'm having a little bit of trouble because my browser is thinking that the %23 is part of the variable that I'm passing.

For example, if I echo $_GET['title'] I get the entire title and then #title right after it. Is there a way to signify the end of passing a variable? Or is there another solution?
0
bswinnertonAuthor Commented:
P.S. this only happens if I type %23 in the browser, if I use the actual pound sign, it works fine
0
miahiCommented:
I'm no master of htaccess, but I think that NE flag is what is missing in your first attempt (the one with the #).

http://httpd.apache.org/docs/2.2/rewrite/rewrite_flags.html (the official doc says you can have that # there)

NE|noescape

By default, special characters, such as & and ?, for example, 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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.