首页 > 解决方案 > PHP中的标头重定向

问题描述

我正在(试图)编写一些代码来根据他们的设备在我的网站内重定向用户。

如果用户在移动设备上,我想在当前路径的开头添加一个目录“/m/”,并将它们重定向到新位置的同一个文件(如果可能,使用相同的 url 参数,但不是必需的)

www.mysite.com/shop/items1/shop.php?para1=somestuff ==> www.mysite.com/m/shop/items1/shop.php?para1=somestuff

我得到的错误是:

无法访问此站点

网页位于

http://dev4/shop/items1/shop.php?uncd=c74f474e2c396da2993cfb344007ad6a&iid=014ec54f3746fd834da71ad21e76a0fa&crtfn=D2

可能暂时关闭或可能....... ERR_UNSAFE_REDIRECT

我的代码是

<?php
 $path = $_SERVER['DOCUMENT_ROOT'];
 include ($path . '/includes/functions.php');
 $pathloc = '';      // Variable used for new folder location  ie /m/ for mobile  /t/ for tablet
 $device= device();  // function to get the type of device
 if ($device=="IsMob")
    {$pathloc = '/m';
    header("Location: " . $path . $pathloc . dirname($_SERVER['SCRIPT_NAME']) . "/" . basename($_SERVER["REQUEST_URI"], ".php"));
         }

?>

我正在使用主机头“dev4”。

我怀疑目录和文件位置有误。我尝试将字符串输出到屏幕以检查正在构建的 url,一切看起来都很好,所以我有点难过。

任何帮助都将不胜感激。

TIA

标签: phpredirectheader

解决方案


As is always the way, as soon as I post the question up I've been struggling with for 2 days, I work out the answer myself.

Reference for anyone it may help.

header("Location: http://Dev4" . $pathloc.dirname($_SERVER['SCRIPT_NAME']). "/". basename($_SERVER["REQUEST_URI"], ".php"));

推荐阅读