Stop phones from accessing subdomain m

A customer asked me to create a subdomain with another website optimized for mobile users. I installed the latest WordPress on this subdomain (eg m.domain.com). This subdomain is accesible to everyone. Only the dummy text and template is available on this subdomain.

Mobile visitors should be visiting the original domain (eg www.domain.com) until the development of the subdomain has been finished. Personally im not very proficient in using WordPress, but I am in using weblanguages like php, mysql, js etc.

Read More

The problem that now came into existance is that some mobile browsers are automatically checking a domain for an m-subdomain. Then they automatically redirect to that subdomain. I did not know this beforehand and im looking for a quick solution. My host doesnt seem to provide an option to temporarily change the name of the subdomain. This means I have to remove the subdomain and create a new one then install wordpress again, develop the website, export the thing, remove the new subdomain, create the m subdomain again, install wordpress again, and import the developed website.

Could there be a simpler solution to this?

Edit
Since there is a need to test this website on mobile phones, the access shouldnt be blocked to mobile phones.

Related posts

Leave a Reply

1 comment

  1. Yes, there is a solution for this, but as Digital Chris mentioned, do not develop under production environment. Do it on localhost instead, or another domain.

    Set up a query parameter, when you first call your page.

    Let it be ?dev=123

    So, in the index php, check this and if it is set, then add a session variable to store that. Then you check, is this session variable exists. If yes, then do nothing, just develop the page. If not, then redirect everybody to the not subdomained domain.

    session_start();
    if (!empty($_GET["dev"]) && $_GET["dev"] == 123) {
        $_SESSION["dev"] = 123;
    }
    
    if (empty($_SESSION["dev"]) || $_SESSION["dev"] != 123) {
        header('Location: http://www.domain.com');
        die();
    }
    

    Of course you can set up more secure parameter name and value. This is not the most secure, way.

    An alternative way could be, if you allow only your IP(s) by checking $_SERVER["REMOTE_ADDR"]; and if it’s not yours, then redirect them back to the non subdomained domain.