Using spéciâl characters in urls

I am developing a website that allows people to create their own translator. They can select the URL name and it is sent to the database and I use .htaccess to redirect website.com/nameoftheirtranslator

in

website.com/translator.php?name=nameoftheirtranslator

Here is my problem:

I recently noticed that someone created a translator with special characters in the name → "LAEFÊVËŠI".

But when it is processed (sent to the php file, then mysqli_real_escape_string ) and added to the database, it looks like just "LAEFVI", so you can see that special characters were lost somewhere.

I'm not quite sure what to do here, but I think there are two ways:

  • Try to save characters and make some encodings (I don’t know where to start)
  • Omit them and tell users to use only “normal” characters in their translator names (not perfect).

I am wondering if there might be a possible URL, such as website.com/LAEFÊVËŠI , which can be interpreted by the server?

EDIT1: I notice that a stack overflow on this very issue translates the special characters in my header to .../using-special-characters-in-urls ! This seems like a great solution, I think I could make a function that translates special characters like â into their usual equivalent (like â)? And I suppose I would just ignore other characters like /#@"',& ? Now that I think about it, there should be some pretty standard / effective strategies to solve such problems.

EDIT2: Actually, now that I think about it (in more detail) - I really want this thing to be used by people of any language (and not just English), so I would really like to be able to have special characters in URLs. Having said that, I just discovered that Google does not interpret â as a , so it may be difficult for people to find the LAEFÊVËŠI translator if I do not translate the letters into regular characters. Ahh!

+4
source share
1 answer

Well, after this crazy episode, here's what happened:

  • Found that I delete all non-alphanumeric characters with PHP preg_replace() .
  • Changed preg_replace , so it removes spaces and uses rawurlencode() :

$name = mysqli_real_escape_string($con, rawurlencode( preg_replace("/\s/", '', $name) ));

  • Now everything in the database is encoded, safe and sounds.
  • This rewrite rule is used. RewriteRule ^([^/.]+)$ process.php?name=$1 [B]
  • Run around in a circle for 2 hours, so my rewriting was wrong because I was getting "page not found"
  • Understand that process.php does not have rawurlencode() to read in name

$name = rawurlencode($_GET['name']);

Now it works.

WOO!

Time to sleep.

+1
source

Source: https://habr.com/ru/post/1488511/


All Articles