【问题标题】:Simple Page Redirect Using URL使用 URL 的简单页面重定向
【发布时间】:2017-08-07 08:28:36
【问题描述】:

我希望我的 index.php 重定向到我的welcome/index.html。我知道有很多方法可以做到这一点,但我想像 Facebook 一样缩短它。我想要一个基于 url 的重定向。我想使用 url 将 index.php 重定向到 welcome/index.html。

例如:

example.com to example.com/?url=welcome
//this 'url=welcome' is the index.html of my welcome folder.

【问题讨论】:

  • 删除不适当的标签、facebook 等
  • 注明@gouravbajaj

标签: javascript php jquery url-redirection


【解决方案1】:

我读到的使用标头进行重定向的地方有点危险,因为黑客可以轻松更改标头,并且黑客可以轻松地将受害者发送到另一个位置。所以我使用 javascript location.href 函数进行重定向。

<?php
echo "<script>window.location ='http://example.com/welcome/index.html</script>";
?>

如果您想从 example.com 将用户发送到动态 url,您可以设置一个 get 变量,例如。

源地址:http//example.com?url=http://example.com/welcome.html 目的地址:http://example.com/welcome.html

<?php
if(isset($_GET['url']))
echo "<script>window.location ='$_GET[url]'</script>";
else
echo "<script>window.location='http://somewhereelse.com'";
?>

现在您可以在源地址中动态设置 url 变量并将用户重定向到您想要的任何地方

【讨论】:

  • 提供这个“我读过的地方”的链接,是什么让使用 javascript 更好或更安全?
  • 请说出你没有得到什么?
  • 假设 example.com/index.php 是您要重定向的页面。 example.com/welcome/index.html 是您的登录页面。因此,在您的 index.php 页面中,您需要有像 example.com/index.php?url=http://example.com/welcome/index.html 这样的变量,这样每当用户来到 index.php 页面时,就会被重定向到 welcome/index.html 页面。 注意:为了提高安全性,您可以在重定向之前验证您的域
  • 在这种情况下,更改 index.php if($_GET['url']==="welcome") echo "
  • 删除所有内容并在下面编写代码。 example.com?url=welcome 你想从哪里重定向对吗?现在在 index.php 中写入 if($_GET['url']==="welcome") echo "
【解决方案2】:

根据我的理解,这是代码。将它放在index.php 的最顶部。我假设您正在使用域example.com

<?php 

$destination = $_GET["url"];

switch($destination) {
    case "welcome": // add your destinations here, one per single "case"
    case "about":
    case "anotherpage":
        header("Location: /" . $destination . "/index.html");
    break;   
    default:
        echo "Error";
    break;
}

?>

通过这种方式,您可以管理哪些重定向有效,哪些无效,避免重定向系统的“邪恶”用法。

example.com?url=http://evilsite.com 这样的恶意内容不会将它们重定向到evilsite.com

这可能不是最好的解决方案,但它是避免不需要的重定向的一个很好的起点。

【讨论】:

  • 我也想在 index.html @Napolux 中隐藏 .html 扩展名
  • 但它实际上并没有回答我的问题@napolux ...我应该使用 javascript 而不是像 6 秒后它会重定向到 welcome/index.html 但你可以在地址中看到的唯一内容栏是 ?url=welcome
  • 你想要JS解决方案还是PHP解决方案。此时问题尚不清楚:(
  • 只要浏览器地址栏上的输出是 ?url=welcome,其中一个就足够了
  • 只要浏览器地址栏上的输出为 ?url=welcome – @Napolux,其中任何一个都足够
【解决方案3】:

不确定这是否会有所帮助,但欢迎您尝试

<?php 
  if (isset($_GET["url"])) {
    $url = $_GET['url'];
    header('Location:'.$url);
  }
 ?>

这个我没试过,但这是我看到你的帖子后的想法

【讨论】:

  • 这真的很危险,因为我可以欺骗用户将他们重定向到不好的地方。示例:example.com?url=http://evilsite.com 会将他们重定向到 evilsite.com
  • 您可以按原样使用它。但正如@Napolux 所说,这是一种非常危险的重定向方式,证明是我收到的反对票
  • 您当然可以“清理”或限制将使用的 url。您可以改用 POST,但根据您的示例,您使用的是 GET
  • @ChevroletSS 忘了问,URL 的最终格式是什么?
  • 啊,如果是这样,那我认为我的回答对你没有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-11
  • 1970-01-01
  • 2018-09-12
相关资源
最近更新 更多