0

My PHP file looks for a URL parameter id such that the URL looks like this:

www.example.com/player.php?id=player_name

where player_name is the id. I cleaned up the URLs using .htaccess so the URL looks as such:

www.example.com/Player/player_name/

and that has been fine except for in the case that I pass in a player ID such as #octothorpe (encoded as %23octothorpe). For some reason, this is still read as a fragment in the URL and ignored by the server, so when the PHP file looks for id, it can't find it. The interesting part is that if I use the ugly URL

www.example.com/player.php?id=%23octothorpe

its works completely fine, but when I do

www.example.com/Player/%23octothorpe

It can't find id

The relevant part of the .htaccess file looks as such:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteRule ^Player/(.*)/$ player.php?id=$1
RewriteRule ^Player/(.*)$ player.php?id=$1
DonutGaz
  • 1,492
  • 2
  • 17
  • 24

2 Answers2

0

The problem is that url fragments are not passed to the apache server. Where you have used it in a query string parameter it's not considered a url fragment which is why it works.

Why the hash part of the URL is not in the server side?

If your id's contain hash characters you will need to think of another strategy for your urls. The solution really depends on the wider context of your application.

Community
  • 1
  • 1
jfxninja
  • 371
  • 3
  • 11
  • I understand the hash is not sent to the server, but I encoded the hash so that it's passed at %23. – DonutGaz Mar 19 '16 at 17:38
0

OP here, I figured out my problem. The issue was that Apache was decoded my URL when it was converted by htaccess, so I used the [B] flag on my rules such that

RewriteRule ^Player/(.*)/$ player.php?id=$1

became

RewriteRule ^Player/(.*)/$ player.php?id=$1 [B]

DonutGaz
  • 1,492
  • 2
  • 17
  • 24