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?
With cookie, create a flag to identify that should not be redirected.
– Daniel Omine
Your url is wrong, you reversed the
&
and the?
, try like thishttp://www.meusite.com.br/?force_desktop=true
– Augusto
True. I switched the symbols. Thank you. I managed to solve my problem with @Talisson code. Thanks for the help!
– Zkk