简体   繁体   English

PHP URL 编码特殊字符失败

[英]PHP URL encoding special characters fails

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.它的链接(来自我不管理的其他网站的内部和外部链接)在停止工作的 URL 中带有重音符号。 Apparently the site used ISO-8859-1 and the new host UTF-8 I'm trying to solve this problem with PHP.显然该站点使用了 ISO-8859-1 和新主机 UTF-8 我正在尝试使用 PHP 解决这个问题

Example of the word "condición" in links looks like:链接中单词“condición”的示例如下所示:

condici%F3n

That returns a 404 but if I write那返回一个 404 但如果我写

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:到目前为止,我设法使用以下代码打印了正确的 URL:

<?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当我尝试传递该词并重定向到 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?我该如何解决这个问题?

<?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()现在用 urldecode() 打印

<?php
     echo urldecode($_GET['value'];
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM