Home » Php » php – Using mod_rewrite to convert paths with hash characters into query strings

php – Using mod_rewrite to convert paths with hash characters into query strings

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have a PHP project where I need to send a hash character (#) within the path of a URL. (http://www.example.com/parameter#23/parameter#67/index.php) I thought that urlencode would allow that, converting the hash to %23

But now I see that even the urlencoded hash forces the browser to treat everything to the right as the URL fragment (or query).

Is there a way to pass a hash through, or do I need to do a character substitution prior to urlencode?

Edit to add (Sep 19 2017):

It turns out that I was asking the wrong question. My issue wasn’t with using the hash character within the path (encoding it does work), but in using mod_rewrite to convert it over to a query string. I had failed to re-encode it within the RewriteRule. I’ll edit the title to match.

Here is the rewrite rule I was using:

RewriteEngine On
# convert path strings into query strings
RewriteRule "^(.*)/(.*)/hashtags.php"      /hashtags.php?parameter_1=$1&parameter_2=$2 [QSA,L]

As soon as I added the B tag, it worked correctly:

RewriteEngine On
# convert path strings into query strings
RewriteRule "^(.*)/(.*)/hashtags.php"      /hashtags.php?parameter_1=$1&parameter_2=$2 [QSA,L,B]
How to&Answers:

Encode the Hash in the URL with %23

http://twitter.com/home?status=I+believe+in+%23love

“I believe in #love”

URL Encoding Reference: http://www.w3schools.com/tags/ref_urlencode.asp