I am currently tying myself up in knots over URL percent-encoding of extended Latin characters.
Step 1 of 4
I have the following URL:
https://example.com/fußgängerbrücke/
The offline folder name (which I have uploaded via FTP) exactly corresponds to this: fußgängerbrücke
Step 2 of 4
Wherever this URL has an internal link pointing to it anywhere on the site, the link now takes the percent-encoded form:
<a href="/fu%C3%9Fg%C3%A4ngerbr%C3%BCcke/">
If I cut and paste the URL from the URL bar in Firefox, it pastes as: fu%C3%9Fg%C3%A4ngerbr%C3%BCcke
Step 3 of 4
But... imagine that URL is a referrer to the current page.
If I now use PHP to grab the URL (and edit it a little):
$My_Reference = str_replace('https://'.$_SERVER['HTTP_HOST'], '', $_SERVER['HTTP_REFERER']);
$My_Reference = explode('?', $My_Reference)[0];
$My_Reference = substr($My_Reference, 1, -1);
and then later, I use $My_Reference
to retrieve some related data:
file_get_contents($_SERVER['DOCUMENT_ROOT'].$My_Reference.'/my-data.php');
it doesn't work.
I had a think about this and concluded that this might be what's happening:
file_get_contents()
is (somehow?) detecting the extended Latin characters in$My_Reference
and parsingfußgängerbrücke
asfu%C3%9Fg%C3%A4ngerbr%C3%BCcke
. (Can this be right?)- It then looks for a folder which literally exists as
/fu%C3%9Fg%C3%A4ngerbr%C3%BCcke/
and can't find it, because the only folder that exists is/fußgängerbrücke/
So, to test this hypothesis, I tried:
file_get_contents($_SERVER['DOCUMENT_ROOT'].urldecode($My_Reference).'/my-data.php');
which does work (hooray!), but... well, it seems weird.
I felt uncomfortable about this, since I'm not trying (and don't need) to decode percent-encoded URLs anywhere else on the site, in any other context, and this just makes for a weird exception. For the sake of consistency, I'd just rather use percent-encoding everywhere.
Step 4 of 4
So... I went back to the original offline folder and renamed it from
fußgängerbrücke
to
fu%C3%9Fg%C3%A4ngerbr%C3%BCcke
and then uploaded it and replaced the old folder.
Guess what...? The new URL doesn't resolve!
Instead, I get a 404
, now.
Presumably because the server is now automatically decoding the hardcoded percent-encoding and trying to find the folder /fußgängerbrücke/
in the webspace... which isn't there.
In summary:
If I upload a folder named fußgängerbrücke
:
example.com/fußgängerbrücke
resolves to/fußgängerbrücke/index.php
/fu%C3%9Fg%C3%A4ngerbr%C3%BCcke
resolves to/fußgängerbrücke/index.php
Whereas if I upload a folder named fu%C3%9Fg%C3%A4ngerbr%C3%BCcke
:
/fußgängerbrücke
goes to a404
/fu%C3%9Fg%C3%A4ngerbr%C3%BCcke
goes to a404
So what am I missing here? Two questions:
- Does
file_get_contents()
automatically percent-encode extended Latin characters which then need to be explicitly percent-decoded again? - Is it impossible to have URL foldernames and filenames which have percent-encoding already hardcoded into them?