Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

.htaccess mobile redirect query

Options
  • 12-07-2012 5:50pm
    #1
    Registered Users Posts: 289 ✭✭


    Hi all,

    Bit of help required if possible!

    Working on a website where we need to apply a redirect to the mobile site. We have code to place in the header, but due to the make up of the site, haven't been able to utilise that. So, instead we have used a .htaccess file to detect the mobile device, and then redirect the user to the mobile site.

    However, we also need a way for the user to be able to click a link on the mobile site, to be able to return to the main full website. So far, I have been unable to do this.

    Below, is the .htaccess file code we are using:
    RewriteEngine on
    RewriteBase /
    
    # Check if mobile=1 is set and set cookie 'mobile' equal to 1
        RewriteCond %{QUERY_STRING} (^|&)mobile=1(&|$)
        RewriteRule ^ - [CO=mobile:1:%{HTTP_HOST}]
    
        # Check if mobile=0 is set and set cookie 'mobile' equal to 0
        RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
        RewriteRule ^ - [CO=mobile:0:%{HTTP_HOST}]
    
        # cookie can't be set and read in the same request so check
        RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
        RewriteRule ^ - [S=1]
    
        # Check if this looks like a mobile device
        RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
        RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC,OR]
        RewriteCond %{HTTP:Profile}       !^$
    
        # Check if we're not already on the mobile site
        RewriteCond %{HTTP_HOST}          !^m\.
        # Check to make sure we haven't set the cookie before
        RewriteCond %{HTTP:Cookie}        !\mobile=0(;|$)
        # Now redirect to the mobile site
        RewriteRule ^ http://m.domain.extn%{REQUEST_URI} [L,R=301]
    

    Any assistance on this would be really helpful.

    Many thanks in advance.

    Paul


Comments

  • Moderators, Category Moderators, Entertainment Moderators, Sports Moderators Posts: 22,584 CMod ✭✭✭✭Steve


    Can you do it in html+JS rather than using .htacess?

    I've used this hack in the past on a dummy entry page: (e.g. mypage.html)
    [HTML]
    <script type="text/javascript">
    <!--
    if (screen.width <= 699){
    document.location = "mymobilepage.html";
    }
    else{
    document.location = "myfullpage.html";
    }
    //-->
    </script>[/HTML]
    The link on the mobile page should then go to myfullpage.html to avoid recursion.

    This also avoids having to use those now legally questionable cookie things...


  • Registered Users Posts: 289 ✭✭paulgrogan.eu


    Steve wrote: »
    Can you do it in html+JS rather than using .htacess?

    I've used this hack in the past on a dummy entry page: (e.g. mypage.html)
    [HTML]
    <script type="text/javascript">
    <!--
    if (screen.width <= 699){
    document.location = "mymobilepage.html";
    }
    else{
    document.location = "myfullpage.html";
    }
    //-->
    </script>[/HTML]The link on the mobile page should then go to myfullpage.html to avoid recursion.

    This also avoids having to use those now legally questionable cookie things...

    Tricky thing is, I haven't been able to find the appropriate header area in the makeup of the pages to place the re-direct code, so using .htaccess bypasses all that requirement.

    Am I supposed to place your code above in the head of the pages?

    Thanks

    Paul


  • Moderators, Category Moderators, Entertainment Moderators, Sports Moderators Posts: 22,584 CMod ✭✭✭✭Steve


    If you place it in the <head></head> section of each page then it will work but will not allow a mobile user back on to the page because the redirect will kick in each time.

    What I was suggesting was to use an entry page for your site which just had the following:
    e.g. if the url is [noparse]"www.mysite.com"[/noparse] and the default page is "index.html" then make index.html contain this:
    [HTML]<!DOCTYPE html >
    <html>
    <head>
    <title>My Page</title>
    <script type="text/javascript">
    <!--
    if (screen.width <= 699){
    document.location = "mymobilepage.html";
    }
    else{
    document.location = "myfullpage.html";
    }
    //-->
    </script>
    </head>
    <body>
    <!-- fallback if javascript is not supported -->
    <p>Welcome to My Page</p><br/>
    <a href="myfullpage.html">Visit our site</a><br/>
    <a href="mymobilepage.html">Visit our mobile site</a><br/>
    </body>
    </html>
    [/HTML]

    You then just need to provide a link to myfullpage.html from your mobile pages.

    This means that you do not need to use cookies and worry about the privacy legislation surrounding them.

    There is probably a better way of doing this using .htaccess but I'm not familiar with php based sites at all, someone else here can probably advise you better on that :)


Advertisement