简体   繁体   English

标头(“ Location:url”)不会重定向

[英]header(“Location: url”) does not redirect

I have been losing a lot of time trying to redirect from my AMP page to another pages. 我一直在浪费很多时间尝试从我的AMP页面重定向到另一个页面。 When a form is submited, a php file is called by xhr-action sending data with POST. 提交表单后,xhr-action会通过POST发送数据来调用php文件。 This php file sets a new location to redirect the page to another one . 这个php文件设置了一个新的位置来将页面重定向到另一个页面 The code i have in the php file to redirect is the next: 我在php文件中重定向的代码是下一个:

<?php 
header("Content-type: application/json");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: https://www.dominio.com/amp/*");
header("AMP-Access-Control-Allow-Source-Origin: https://www.dominio.com");
header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");
$especi=$_POST["especialidad"];
$paises=$_POST["ES"];
$paismx=$_POST["MX"];
$paispr=$_POST["PR"];
$paiscl=$_POST["CL"];
$paisco=$_POST["CO"];
$paisar=$_POST["AR"];
if(isset($paises)){
    $urlbase="https://www.dominio.es/p/";
    $pais="ES";
    $provin=$paises;
    $pobla=0; //$_POST["zn2"];
}
/*
else if(isset($paismx)){
    $urlbase="https://www.dominio.mx/p/";
    $pais="MX";
    $provin=$paismx;
    $pobla=$_POST["zn2"];
}
else if(isset($paispr)){
    $urlbase="https://www.dominio.com/p/";
    $pais="PR";
    $provin=$paispr;
    $pobla=$_POST["zn2"];
}
else if(isset($paiscl)){
    $urlbase="https://www.dominio.cl/p/";
    $pais="CL";
    $provin=$paiscl;
    $pobla=$_POST["zn2"];
}
else if(isset($paisco)){
    $urlbase="https://www.dominio.co/p/";
    $pais="CO";
    $provin=$paisco;
    $pobla=$_POST["zn2"];
}
else if(isset($paisar)){
    $urlbase="https://www.dominio.com.ar/p/";
    $pais="AR";
    $provin=$paisar;
    $pobla=$_POST["zn2"];
}*/


$queryespe="SELECT * FROM tx WHERE Id='$especi'";
$resultespe = mysqli_query($link,$queryespe);
$fila = mysqli_fetch_array($resultespe);
$especialidad=$fila['Keyword'];
$especialidad=reemplazargui($especialidad);
$especialidad=reemplazarac($especialidad);
if($pobla!=0){
    $querypo="SELECT * FROM ts WHERE Id='$pobla'";
    $resultpo = mysqli_query($link,$querypo);
    $filapo = mysqli_fetch_array($resultpo);
    $poblacion=reemplazargui($filapo['Keyword']);
    $ulr=$urlbase.$especialidad."/".reemplazarac($poblacion)."/";
    mysqli_close($link);
    header ("Location: $ulr");
    die();
}else{
    $querypo="SELECT * FROM tx_2 WHERE Id='$provin'";
    $resultpo = mysqli_query($link,$querypo);
    $filapo = mysqli_fetch_array($resultpo);
    $poblacion=reemplazargui($filapo['Keyword']);
    $ulr=$urlbase.$especialidad."/".reemplazarac($poblacion)."/";
    mysqli_close($link);
    $ulr = html_entity_decode($ulr);
    header ("Location: $ulr");
    die();
    }

?>

The process arrives to the entity_decode correctly and the url (ulr) i put inside header is correct. 该过程正确到达了entity_decode,并且我放在标头中的URL(ulr)是正确的。 The response i get from chrome network analysis is the next (i do not understand why exists two initiators for the same url that, in addition, is not working). 我将从chrome网络分析获得的响应是​​下一个响应(我不明白为什么对于同一个URL存在两个启动器,而且这些启动器不起作用)。 Error Log: 错误日志:

Fetch API cannot load https://www.dominio.com/amp/include/listado.php?__amp_source_origin=http%3A%2F%2Fwww.dominio.com . 提取API无法加载https://www.dominio.com/amp/include/listado.php?__amp_source_origin=http%3A%2F%2Fwww.dominio.com Redirect from ' https://www.dominio.com/amp/include/listado.php?__amp_source_origin=http%3A%2F%2Fwww.dominio.com ' to ' https://www.dominio.es/m/Alergologo/Albacete-Provincia/ ' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value ' https://www.dominio.com/amp/ *' that is not equal to the supplied origin. 从' https://www.dominio.com/amp/include/listado.php?__amp_source_origin=http%3A%2F%2Fwww.dominio.com '重定向到' https://www.dominio.es/m/Alergologo / Albacete-Provincia /已被CORS政策阻止:“ Access-Control-Allow-Origin”标头的值“ https://www.dominio.com/amp/ *”不等于提供的来源。 Origin ' http://www.dominio.com ' is therefore not allowed access. 因此,不允许访问来源“ http://www.dominio.com ”。 Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. 让服务器发送带有有效值的标头,或者,如果不透明的响应满足您的需要,请将请求的模式设置为“ no-cors”,以在禁用CORS的情况下获取资源。

Fetch failed https://www.dominio.com/amp/include/listado.php?__amp_source_origin=http%3A%2F%2Fwww.dominio.com : Failed to fetch 提取失败https://www.dominio.com/amp/include/listado.php?__amp_source_origin=http%3A%2F%2Fwww.dominio.com :无法提取

Uncaught Error: Form submission failed:: Fetch failed https://www.dominio.com/amp/include/listado.php?__amp_source_origin=http%3A%2F%2Fwww.dominio.com : Failed to fetch​​​ reported at na.f.assert (log.js:295) at xhr-impl.js:164 未捕获的错误:表单提交失败::下载失败https://www.dominio.com/amp/include/listado.php?__amp_source_origin=http%3A%2F%2Fwww.dominio.com :无法抓取报道 na.f.assert(log.js:295)在xhr-impl.js:164

So, i need to redirect to another page calling from AMP page to the php file i am showing, which must redirect to final page. 因此,我需要重定向到从AMP页面调用到我显示的php文件的另一个页面,该页面必须重定向到最终页面。 The redirecting is not working with this code . 重定向不适用于此代码

Edit: The problem could be the CORS policy, but i have added this lines to htaccess file and keeps without working. 编辑:问题可能是CORS策略,但我已将这行添加到htaccess文件中,并且一直无法正常工作。

<IfModule mod_headers.c>
    <FilesMatch "\.(ttf|ttc|otf|eot|woff|font.css|css)$">
        Header set Access-Control-Allow-Origin "*"
    </FilesMatch>
</IfModule>

Edit 2: Modified headers: 编辑2:修改后的标头:

ob_start();
header("Content-type: application/json");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: https://www.dominio.com/amp/*");
header("AMP-Access-Control-Allow-Source-Origin: https://www.dominio.com/amp/*");
header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");

Errors: 错误:

Fetch API cannot load https://www.dominio.com/amp/include/listado.php?__amp_source_origin=http%3A%2F%2Fwww.dominio.com . 提取API无法加载https://www.dominio.com/amp/include/listado.php?__amp_source_origin=http%3A%2F%2Fwww.dominio.com Redirect from ' https://www.dominio.com/amp/include/listado.php?__amp_source_origin=http%3A%2F%2Fwww.dominio.com ' to ' https://www.dominio2.es/m/Alergologo/Alicante-Provincia/ ' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value ' https://www.dominio.com/amp/ *' that is not equal to the supplied origin. 从' https://www.dominio.com/amp/include/listado.php?__amp_source_origin=http%3A%2F%2Fwww.dominio.com '重定向到' https://www.dominio2.es/m/Alergologo / Alicante-Provincia /已被CORS政策阻止:“ Access-Control-Allow-Origin”标头的值“ https://www.dominio.com/amp/ *”不等于提供的来源。 Origin ' http://www.dominio.com ' is therefore not allowed access. 因此,不允许访问来源“ http://www.dominio.com ”。 Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. 让服务器发送带有有效值的标头,或者,如果不透明的响应满足您的需要,请将请求的模式设置为“ no-cors”,以在禁用CORS的情况下获取资源。

Edit 3: SOLVED 编辑3:解决

The adition of the headers to allow CORS have solved the problem. 标题的添加允许CORS解决了该问题。 They must be added in all afected domains. 必须将它们添加到所有受影响的域中。

header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: https://www.dominio.com/amp/*");
header("AMP-Access-Control-Allow-Source-Origin: https://www.dominio.com/amp/*");
header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");

The solution is added to the post, Edit 3. 该解决方案已添加到帖子Edit 3中。

The adition of the headers to allow CORS have solved the problem. 标题的添加允许CORS解决了该问题。 They must be added in all afected domains. 必须将它们添加到所有受影响的域中。

header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: https://www.dominio.com/amp/*");
header("AMP-Access-Control-Allow-Source-Origin: https://www.dominio.com/amp/*");
header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");
    REPLACE,
    header ("Location: $ulr");
    die();
    WITH,
    header ("Location: $ulr");
    ?>
    <script type="text/javascript">
    location.href = '<?php echo $ulr ?>';
    </script>
    <?php
    die();

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

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