是否可以通过使用PHP将用户重定向到不同的页面?

假设用户转到www.example.com/page.php,我想将其重定向到www.example.com/index.php,那么在不使用元刷新的情况下,我如何做到这一点?有可能吗?

这甚至可以保护我的页面免受未授权用户的攻击。


当前回答

header("Location: https://www.example.com/redirect.php");

直接重定向到此链接https://www.example.com/redirect.php

$redirect = "https://www.example.com/redirect.php";
header("Location: $redirect");

首先获取$redirect值,然后重定向到[value],如:https://www.example.com/redirect.php

其他回答

您可以尝试使用

header('Location:'.$your_url)

有关更多信息,请参阅php官方文档

试试这个

 $url = $_SERVER['HTTP_REFERER'];
 redirect($url);

以下是我的想法:

IMHO,重定向传入请求的最佳方法是使用位置标头

<?php
    header("Location: /index.php");
?>

一旦执行了该语句并发出输出,浏览器将开始重新引导用户。但是,请确保在发送头之前没有任何输出(任何echo/var_dump),否则会导致错误。

虽然这是一种快速而肮脏的方式来实现最初的要求,但最终会成为一场SEO灾难,因为这种重定向总是被解释为301/302重定向,因此搜索引擎将始终将您的索引页视为重定向页,而不是登录页/主页。

因此,它会影响网站的SEO设置。

是的,可以使用PHP。我们将重定向到另一个页面。

尝试以下代码:

<?php
    header("Location:./"); // Redirect to index file
    header("Location:index.php"); // Redirect to index file
    header("Location:example.php");
?>

我们可以通过两种方式实现:

当用户启动时https://bskud.com/PINCODE/BIHAR/index.php然后重定向到https://bskud.com/PINCODE/BIHAR.php通过以下PHP代码<?php文件header(“位置:https://bskud.com/PINCODE/BIHAR.php");出口?>将上述代码保存在https://bskud.com/PINCODE/BIHAR/index.php如果任何条件为真,则重定向到另一页:<?php文件$myVar=“bskud”;如果($myVar==“bskud”){?><script>window.location.href=“https://bskud.com“;</script><?php文件}其他{echo“<b>再次检查网站名称</b>”;}?>