-1

I moved my website to a new host. It has links (both internal and external from other websites I don't manage) with accents in the URLs which stopped working. Apparently the site used ISO-8859-1 and the new host UTF-8 I'm trying to solve this problem with PHP.

Example of the word "condición" in links looks like:

condici%F3n

That returns a 404 but if I write

condici%C3%B3n

OR

condición

on the address bar, it works fine.

So far I managed to print the right URL with the following code:

<?php
mb_internal_encoding("iso-8859-1");
mb_http_output( "iso-8859-1" );
ob_start("mb_output_handler");
$value  = $_GET['termino'];
echo $value;
?>
https://example.com/page.php?termino=condici%F3n
  • Prints condición

The problem comes when I try to pass that word and redirect to an URL

<?php
mb_internal_encoding("iso-8859-1");
mb_http_output( "iso-8859-1" );
ob_start("mb_output_handler");
$value  = $_GET['termino'];
header("Location: https://example.com/$value", true, 301);
?>

It switches back to:

condici%F3n

Why is this happening? How can I solve this?

2
  • 1
    "Why is this happening?" - because you still put just the value condición into your URL, and leave it up to the client to apply URL encoding ...? Commented Sep 6, 2022 at 14:40
  • But if I "put just the value condición into URL" it works. Only when the script does it fails Commented Sep 7, 2022 at 17:04

1 Answer 1

0
<?php
   mb_internal_encoding("iso-8859-1");
   mb_http_output( "iso-8859-1" );
   ob_start("mb_output_handler");
   $value  = urlencode($_GET['termino']);
   header("Location: https://example.com/$value", true, 301);
?>

And Now print with urldecode()

<?php
     echo urldecode($_GET['value'];
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. Please note my goal is to fix links, I was printing as a way of debugging only.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.