How to redirect website to desktop version?

Asked

Viewed 1,790 times

1

My site has two versions: Desktop and Mobile.

When I access by mobile "www.meusite.com.br", I am redirected to the mobile version: "m.meusite.com.br".

To do this, I’m using this really cool project called Mobile Detect

So far so good.

The site in the Mobile version, has a button to access the Desktop version "Switch to Desktop version". And here comes the problem.

When I click on the button to access the Desktop version, I am redirected from "m.meusite.com.br" to "www.meusite.com.br". Page loaded and script Mobile Detect again it loads and I am redirected to "m.meusite.com.br" again. That is, it loops.

To illustrate it better:

---> "Switch to Desktop" button is clicked;
---> The page is redirected to "www.meusite.com.br"; ---> The "www" page is loaded; ---> Since I’m on my cell phone and I’m on "www.", the Mobile Detect script is loaded again and I go back to "m.meusite.com.br";

Site code version Desktop:

<?php
  require_once 'Mobile_Detect.php';
  $detect = new Mobile_Detect;
?>
<!DOCTYPE html>
<html>
 <head>
<meta charset="utf8">
  <?php if( $detect->isMobile() ) : ?>

    <script type="text/javascript">
         window.location.href = "http://m.meusite.com.br";  
    </script>

  <?php endif ?>

 </head>
<body style="padding: 0; margin: 0">

<div class="wrapper" style="height: 100vh; display: flex; justify-content: center; align-items: center; flex-direction: column">
  <h1>VERSÃO DESKTOP</h1>      
</div>
 </body>
</html>

Site code version Mobile:

<!DOCTYPE html>
<html>
<head></head>
<body style="padding: 0; margin: 0">
    <div class="wrapper" style="height: 100vh; display: flex; justify-content: center; align-items: center; flex-direction: column">
      <h1>VERSÃO MOBILE</h1>
      <?php    
          require_once "../Mobile_Detect.php";
          $detect = New Mobile_Detect;

           if( $detect->isMobile() && (!isset($_GET['force_desktop']) || $_GET['force_desktop'] == 'false')) : ?>
               <a href="http://www.meusite.com.br&force_desktop=true">REDIRECIONAR PARA DESKTOP</a>
            <?php endif; ?>
    </div>
</body>
</html>

Thinking of a logic of forcing the page to stay in the desktop version when I’m by mobile, I’m using this code inside the button that redirect to the desktop:

 if( $detect->isMobile() && (!isset($_GET['force_desktop']) || $_GET['force_desktop'] == 'false')) : ?>
     <a href="http://www.meusite.com.br&force_desktop=true?">REDIRECIONAR PARA DESKTOP</a>
 <?php endif; ?>

However, it gives page error not found. I think I’m missing the parameter to pass in the URL.

How can I fix this? How can I force redirect to the Desktop version when I’m on mobile?

  • 1

    With cookie, create a flag to identify that should not be redirected.

  • 1

    Your url is wrong, you reversed the & and the ? , try like this http://www.meusite.com.br/?force_desktop=true

  • True. I switched the symbols. Thank you. I managed to solve my problem with @Talisson code. Thanks for the help!

1 answer

-1


You can check if your GET value exists, if it exists the device check is not done.

<?php
  require_once 'Mobile_Detect.php';
  $detect = new Mobile_Detect;
?>
<!DOCTYPE html>
<html>
 <head>
<meta charset="utf8">

  <?php if( !isset($_GET['force_desktop']) && empty($_GET['force_desktop'])) : ?>

  <?php if( $detect->isMobile() ) : ?>

    <script type="text/javascript">
         window.location.href = "http://m.meusite.com.br";  
    </script>

  <?php endif ?>
  <?php endif ?>
  <?php var_dump($_GET); ?>
 </head>
<body style="padding: 0; margin: 0">

<div class="wrapper" style="height: 100vh; display: flex; justify-content: center; align-items: center; flex-direction: column">
  <h1>VERSÃO DESKTOP</h1>     
</div>
 </body>
</html>
  • Thanks for the help man! What a silly mistake. I was doing the check on the page on index of the "m.meusite.". The right thing was to do in the index desktop. And of course, before the condition that checks if I am on a mobile device. Very simple, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.