简体   繁体   中英

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 . But when I type in the URL https://growtopiajaw.my.vg , it will keep refreshing in an infinite loop.

I know that some people will try to access the https://growtopiajaw.my.vg page.

I don't want my site to be problematic for users. Also, you can try visiting the site 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 . I currently have the code below in 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. (I cannot post more than 8 links. sorry)

So there are two possible scenarios that may apply here...

Scenario 1 - Redirect all HTTP traffic to HTTPS

You can achieve this by simply adding an .htaccess to the root of your project with the following contents...

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)

Source: GoDaddy

Scenario 2 - Redirect (only) the landing page to HTTPS

A landing page is basically 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:

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

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

For simplicity sake, let's say your default landing page is index.html .

In which case, simply create index.html in the root of your project and add the following--

<?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

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.

Place a php file named index.php in your root directory. 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

I suggest you rename homepage.html to index.php and then add the following code above the doctype tag

<?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. Otherwise use what @Rushikumar has suggested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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