Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Redirect only a single webpage to some other webpage based on User Agent

I'm making an Android app (can't publish it to Play Store because my parents won't let me pay the developer fee) and so, I want the users to be able to check for app updates from within the app.

I have thought of my own way for that because I wasn't able to implement the other ways I found on the internet.

Now, I have a version checking page: "https://app.brokenhearts.ml/check-update.html".
Also, there's an download update page: "https://app.brokenhearts.ml/updated.html".

I've set a custom User Agent to the app "Broken Hearts/1.0". That's working fine as of now, as when websites are loaded from the app they're displaying that custom user agent. I'll be updating the User Agent of the app with every version (for example, app version 2.0 will have the User Agent "Broken Hearts/2.0").

So, whenever a user will check for update, the app is going to load "https://app.brokenhearts.ml/check-update.html" in the webview (with the User Agent "Broken Hearts/1.0"). Now when that's the latest update, I won't have added any special .htaccess rules to redirect it and so, the app will normally load "https://app.brokenhearts.ml/check-update.html". But, suppose I release v2.0, the users still on v1.0, will still be having their user agents set to "Broken Hearts/1.0", and thus, when checking for the update, they shall be redirected to "https://app.brokenhearts.ml/updated.html".

So, basically, whenever I release an update to the app, I'll be updating its User Agent and changing the .htaccess to redirect the users from the old user agent to the "download update page". So, users from any other user agent should remain unaffected and should be able to load the "version checking page" normally.

All of this is going to be based on User Agent and .htaccess. I think it's possible, however, I'm just not able to make the correct .htaccess code.

Comments

  • I think it might be possible with .htaccess rules, but I personally would avoid the headache of .htaccess rules and just use PHP code for this. So rename the files from .html to .php and add a snippet like this to the top:

    <?php
    
    define('LATEST_APP_VERSION', 2.0);
    
    function get_app_version() {
         $matches = [];
         $result = preg_match('/^Broken Hearts\/(\d+\.\d+)/', $_SERVER['HTTP_USER_AGENT'], $matches);
         if (!$result) {
            return null;
        } else {
            return floatval($matches[1]);
    }
    
    if (get_app_version() < LATEST_APP_VERSION) {
        echo "Your app is out of date, using version: ".get_app_version();
        die();
    }
    
  • I wish I could use it. Actually, I want to make a full webpage and I have it all ready in Adobe Muse. It exports only as HTML. Thus, I just wanted a working .htaccess. Also, I'm not quite familiar with php and so, to customize it, will be a task for me.

    Till now, all have given me 500 error. I need some kind of tutorial that will explain .htaccess to me instead of just copying-pasting it.

    This is my most current one while writing this:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_USER_AGENT} ^(Broken Hearts/1.0)$
    RewriteRule ^(https://app.brokenhearts.ml/check-version.html)
    https://app.brokenhearts.ml/updated.html [R=301,]

    Any improvisation, maybe?

  • This is the latest one I found out from Apache docs:

    RewriteCond "%{HTTP_USER_AGENT}" "(Broken Hearts/1.0)"
    RewriteRule "^/$" "/updated.html" [L]

    RewriteRule "^/$" "/index.html" [L]

    The URL of the docs:

    https://httpd.apache.org/docs/current/mod/mod_rewrite.html

    Quoted from that page:

    To rewrite the Homepage of a site according to the ``User-Agent:'' header of the request, you can use the following:

    RewriteCond "%{HTTP_USER_AGENT}" "(iPhone|Blackberry|Android)"
    RewriteRule "^/$" "/homepage.mobile.html" [L]

    RewriteRule "^/$" "/homepage.std.html" [L]

    Explanation: If you use a browser which identifies itself as a mobile browser (note that the example is incomplete, as there are many other mobile platforms), the mobile version of the homepage is served. Otherwise, the standard page is served.

    Now my point is, I tried changing my user agent through the developer tools and still, nothing changed. At least, I'm not getting 500 error anymore, but the redirect isn't working.

  • FYI: in Markdown you can format a multiline block of code by having a line with three backticks (`) above and below the block of code.

    But I think this should work:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_USER_AGENT} "^(Broken Hearts/1\.0)$"
    RewriteRule "^/check-version\.html" /updated.html [R=301,]
    

    I believe the RewriteRule should only have the path of the URL, not the domain name and protocol.

  • Thank you for the code, @Admin

    However, that just gives an error: "500 Error, please check your php script / enable display_errors in your cpanel".

    I've now enabled the logs, but, they might take an hour to appear.

  • Well, I found a solution!

    I created a folder called "update" and placed the .htaccess in it. Modified it as folows:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_USER_AGENT} "^(Broken Hearts/1\.0)$"
    RewriteRule ^$ https://app.brokenhearts.ml/updated.html [R,L]

    RewriteRule ^$ https://app.brokenhearts.ml/check-version.html [L]

    And I pointed the update checker in the app to https://app.brokenhearts.ml/update/.

    So, now, if the user agent matches my custom one, it's getting redirected correctly to updated and if it's the latest one, it's getting redirected to the check version as expected.

    Now, whenever I'll publish an update, I'll have to modify the .htaccess when the new rule as follows:

    Suoopse, I've posted v3.0 update and users are still using 1.0 and 2.0, then my new .htaccess will be:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_USER_AGENT} "^(Broken Hearts/1\.0)$"
    RewriteRule ^$ https://app.brokenhearts.ml/updated.html [R,L]

    RewriteCond %{HTTP_USER_AGENT} "^(Broken Hearts/2\.0)$"
    RewriteRule ^$ https://app.brokenhearts.ml/updated.html [R,L]

    RewriteRule ^$ https://app.brokenhearts.ml/check-version.html [L]

    I have tried it and it works. Posted it just hoping some other day it might help someone else.

  • @VPHrishikesh said:
    Well, I found a solution!

    I created a folder called "update" and placed the .htaccess in it. Modified it as folows:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_USER_AGENT} "^(Broken Hearts/1\.0)$"
    RewriteRule ^$ https://app.brokenhearts.ml/updated.html [R,L]

    RewriteRule ^$ https://app.brokenhearts.ml/check-version.html [L]

    And I pointed the update checker in the app to https://app.brokenhearts.ml/update/.

    So, now, if the user agent matches my custom one, it's getting redirected correctly to updated and if it's the latest one, it's getting redirected to the check version as expected.

    Now, whenever I'll publish an update, I'll have to modify the .htaccess when the new rule as follows:

    Suoopse, I've posted v3.0 update and users are still using 1.0 and 2.0, then my new .htaccess will be:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_USER_AGENT} "^(Broken Hearts/1\.0)$"
    RewriteRule ^$ https://app.brokenhearts.ml/updated.html [R,L]

    RewriteCond %{HTTP_USER_AGENT} "^(Broken Hearts/2\.0)$"
    RewriteRule ^$ https://app.brokenhearts.ml/updated.html [R,L]

    RewriteRule ^$ https://app.brokenhearts.ml/check-version.html [L]

    I have tried it and it works. Posted it just hoping some other day it might help someone else.

    Nice, if it works for you it's okay.
    I couldn't help but notice that most of your site's URLs return a 404 error and the website seems broken, is this an issue you know?
    Thanks.

  • @ChrisPAR

    Yes, I'm aware of it. Those were just test pages that I had copied and pasted there to check if this .htaccess thing works out. I'm working on the website and will be uploading it to the server once it's done.

Sign In or Register to comment.