繁体   English   中英

如何将用户从一个网页或另一个网页重定向到该网页

[英]How can I redirect users from a webpage or another webpage to a webpage

我需要这样的东西

<?php
  if http://growtopiajaw.my.vg
  else if https://growtopiajaw.my.vg
  then
  header('Location: https://growtopiajaw.my.vg/homepage.html', true, 301);
  exit();
?>

基本上,如果我输入URL growtopiajaw.my.vg,它将自动重定向到https://growtopiajaw.my.vg/homepage.html 但是当我输入URL https://growtopiajaw.my.vg时 ,它将无限循环刷新。

我知道有些人会尝试访问https://growtopiajaw.my.vg页面。

我不希望我的网站对用户有问题。 另外,您可以尝试访问站点https://www.growtopiajaw.my.vg 您会看到它不断刷新页面。

因此,我正在寻求可以帮助我的任何人的帮助。 谢谢!

编辑:

好的,我的问题还不清楚。 我真正的意思是这样的。 http://growtopiajaw.my.vghttps://growtopiajaw.my.vg (如果用户转到此URL)重定向到https上的特定页面,从而创建链接https://growtopiajaw.my.vg/homepage .html 我目前在http://growtopiajaw.my.vg/index.html中具有以下代码。

<?php header('Location: https://growtopiajaw.my.vg/homepage.html', true, 301); exit(); ?>

服务器将自动加载index.html,因此它将重定向到页面https:/growtopiajaw.my.vg/homepage.html。 (我发布的链接不能超过8个。对不起)

因此,这里有两种可能的情况适用...

方案1-将所有HTTP流量重定向到HTTPS

您可以通过简单地将.htaccess添加到项目的根目录中来实现以下目的:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

请注意此处的重定向类型...它是301 ,它转换为永久重定向(vs. 302 ,它转换为临时重定向)

资料来源:GoDaddy

方案2-将登录页面重定向(仅)到HTTPS

登陆页面基本上是PAGE_NAME.EXT 它可以有多种形式...例如,请考虑以下来自托管服务提供商的摘录-

我们的Web服务器按以下顺序查找索引文件:

index.html index.htm index.shtml index.php index.php5 index.php4 index.php3 index.cgi default.html default.htm home.html home.htm Index.html Index.htm Index.shtml Index.php索引。 cgi Default.html Default.htm Home.html Home.htm placeholder.html

如果您的网站的顶层包含具有任何上述名称的文件,则在访客未指定文件名时将显示该文件。

资料来源:TigerTech

为了简单起见,假设您的默认登录页面是index.html

在这种情况下,只需在项目的根目录中创建index.html并添加以下内容-

<?php
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: https://growtopiajaw.my.vg/homepage.html");
    exit();
?>

现在,任何加载//growtopiajaw.my.vg尝试都应将用户带到https://growtopiajaw.my.vg/homepage.html

但是请注意,仅当用户输入growtopiajaw.my.vg URL时,这才会重定向-如果他们进入growtopiajaw.my.vg/about.html ,则无疑会将其带到该页面的HTTP版本。

在您的根目录中放置一个名为index.php的php文件。 在其中编写代码。

<?php
  header('Location: https://growtopiajaw.my.vg/homepage.html');
  exit;

现在,所有对http://growtopiajaw.my.vghttps://growtopiajaw.my.vg请求都将重定向到https://growtopiajaw.my.vg/homepage.html

我建议您将homepage.html重命名为index.php,然后在doctype标记上方添加以下代码

<?php
    if( !isset( $_SERVER['HTTPS'] ) ) {
        header( "location: https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" );
        exit();
    }
?>

这都是基于无法在服务器本身上设置301规则。 否则,请使用@Rushikumar的建议。

暂无
暂无

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

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