简体   繁体   English

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

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

I need something like this 我需要这样的东西

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

Basiclly, if I type in the URL growtopiajaw.my.vg, it will automaticlly redirect to https://growtopiajaw.my.vg/homepage.html . 基本上,如果我输入URL growtopiajaw.my.vg,它将自动重定向到https://growtopiajaw.my.vg/homepage.html But when I type in the URL https://growtopiajaw.my.vg , it will keep refreshing in an infinite loop. 但是当我输入URL https://growtopiajaw.my.vg时 ,它将无限循环刷新。

I know that some people will try to access the https://growtopiajaw.my.vg page. 我知道有些人会尝试访问https://growtopiajaw.my.vg页面。

I don't want my site to be problematic for users. 我不希望我的网站对用户有问题。 Also, you can try visiting the site https://www.growtopiajaw.my.vg . 另外,您可以尝试访问站点https://www.growtopiajaw.my.vg You can see that it keeps refreshing the page non-stop. 您会看到它不断刷新页面。

So, I am seeking help from anyone who can help me. 因此,我正在寻求可以帮助我的任何人的帮助。 Thank you! 谢谢!

EDIT: 编辑:

Okay, so my question is not quite clear. 好的,我的问题还不清楚。 What I actually meant was something like this. 我真正的意思是这样的。 Redirect http://growtopiajaw.my.vg and https://growtopiajaw.my.vg (if the user went to this URL) to a specific page on https which makes the link https://growtopiajaw.my.vg/homepage.html . http://growtopiajaw.my.vghttps://growtopiajaw.my.vg (如果用户转到此URL)重定向到https上的特定页面,从而创建链接https://growtopiajaw.my.vg/homepage .html I currently have the code below in http://growtopiajaw.my.vg/index.html . 我目前在http://growtopiajaw.my.vg/index.html中具有以下代码。

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

The server will automaticlly load the index.html so it will redirect to the page https:/growtopiajaw.my.vg/homepage.html. 服务器将自动加载index.html,因此它将重定向到页面https:/growtopiajaw.my.vg/homepage.html。 (I cannot post more than 8 links. sorry) (我发布的链接不能超过8个。对不起)

So there are two possible scenarios that may apply here... 因此,这里有两种可能的情况适用...

Scenario 1 - Redirect all HTTP traffic to HTTPS 方案1-将所有HTTP流量重定向到HTTPS

You can achieve this by simply adding an .htaccess to the root of your project with the following contents... 您可以通过简单地将.htaccess添加到项目的根目录中来实现以下目的:

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

Notice the type of redirect here... it is 301 which translates to permanent redirect (vs. 302 , which translates to temporary redirection) 请注意此处的重定向类型...它是301 ,它转换为永久重定向(vs. 302 ,它转换为临时重定向)

Source: GoDaddy 资料来源:GoDaddy

Scenario 2 - Redirect (only) the landing page to HTTPS 方案2-将登录页面重定向(仅)到HTTPS

A landing page is basically PAGE_NAME.EXT . 登陆页面基本上是PAGE_NAME.EXT This can have many forms... for example, consider the following excerpt from a hosting provider-- 它可以有多种形式...例如,请考虑以下来自托管服务提供商的摘录-

Our Web servers look for index files in this order: 我们的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 Index.cgi Default.html Default.htm Home.html Home.htm placeholder.html 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

If the top level of your website contains a file with any of those names, that file will be shown when visitors don't specify a file name. 如果您的网站的顶层包含具有任何上述名称的文件,则在访客未指定文件名时将显示该文件。

Source: TigerTech 资料来源:TigerTech

For simplicity sake, let's say your default landing page is index.html . 为了简单起见,假设您的默认登录页面是index.html

In which case, simply create index.html in the root of your project and add the following-- 在这种情况下,只需在项目的根目录中创建index.html并添加以下内容-

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

Now, any attempts to load //growtopiajaw.my.vg should take the user to https://growtopiajaw.my.vg/homepage.html 现在,任何加载//growtopiajaw.my.vg尝试都应将用户带到https://growtopiajaw.my.vg/homepage.html

Notice though, this will redirect ONLY IF the user enters the growtopiajaw.my.vg URL --- if they go to growtopiajaw.my.vg/about.html then it will undoubtedly take them to a HTTP version of such a page. 但是请注意,仅当用户输入growtopiajaw.my.vg URL时,这才会重定向-如果他们进入growtopiajaw.my.vg/about.html ,则无疑会将其带到该页面的HTTP版本。

Place a php file named index.php in your root directory. 在您的根目录中放置一个名为index.php的php文件。 Write your code in it. 在其中编写代码。

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

Now all requests to http://growtopiajaw.my.vg and https://growtopiajaw.my.vg will be redirected to https://growtopiajaw.my.vg/homepage.html 现在,所有对http://growtopiajaw.my.vghttps://growtopiajaw.my.vg请求都将重定向到https://growtopiajaw.my.vg/homepage.html

I suggest you rename homepage.html to index.php and then add the following code above the doctype tag 我建议您将homepage.html重命名为index.php,然后在doctype标记上方添加以下代码

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

This is all based on not being able to setup 301 rules on the server itself. 这都是基于无法在服务器本身上设置301规则。 Otherwise use what @Rushikumar has suggested. 否则,请使用@Rushikumar的建议。

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

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