The site is under maintenance.
Posts

How to permanently remove ?m=1 from Blogger Blog

Many of you asked me a lot of times about getting rid of the 'm' parameter from blogger blog url. Even though there is no problem with this parameter, people want to remove it from the url.

Currently there is no native way to remove it, but there is a workaround to do this. Using Cloudflare Workers as middleware, we can modify the response before it reaches to user. What we can do is detect the device type (i.e. 'mobile', 'tablet' or 'desktop') using 'User-Agent' request header and fetch the origin with 'm' parameter with value based on the device type ('0' for 'desktop' and '1' for others) and send it back to the user. By doing this, user will no longer get redirected to the url with 'm' parameter when visited on mobile devices.

This process requires Custom domain routed to a Cloudflare Workers which means you need a Custom Domain integrated with Cloudflare. Therefore, we can't do the same for .blogspot subdomain.

Limitations

Before we begin, you must be aware about Cloudflare Workers Limits. If you hit these limits, the site will not be accessible. To solve this issue, you may consider switching to Paid plan.

Requirements

Before we start, there are several things which must be required:

  1. DNS must be managed by Cloudflare (Note: Proxy must be enabled).

Creating Workers in Cloudflare

  1. Login to your Cloudflare Account.
  2. Go to Workers & Pages section and click on Create application.
  3. Go to Workers tab and click on Create Worker and rename the worker as remove-m-worker.
  4. Click on Deploy as we shall be able to edit code after deploying "Hello World!" worker.
  5. Now click on Edit code and replace the existing code with the following code:
    /**
     * Environment interface
     * 
     * @typedef Env
     * @property {string} my_var
     */
    
    /**
     * A helper function to get the device type from user agent
     * 
     * @param {string | null} userAgent
     * 
     * @returns {"mobile" | "tablet" | "desktop"}
     */
    const getDeviceType = (userAgent) => {
      const mobileRegexp = /(?:phone|windows\s+phone|ipod|blackberry|(?:android|bb\d+|meego|silk|googlebot) .+? mobile|palm|windows\s+ce|opera\ mini|avantgo|mobilesafari|docomo|KAIOS)/i;
      const tabletRegexp = /(?:ipad|playbook|(?:android|bb\d+|meego|silk)(?! .+? mobile))/i;
    
      if (typeof userAgent === "string") {
        if (mobileRegexp.test(userAgent)) {
          return "mobile";
        }
    
        if (tabletRegexp.test(userAgent)) {
          return "tablet";
        }
      }
    
      // Everything else not matched above will be considered as desktop
      return "desktop";
    }
    
    /**
     * An object with workers handlers
     * 
     * @type {ExportedHandler<Env>}
     */
    const worker = {
      async fetch(request, env, context) {
        // Get the device type from user-agent header
        const deviceType = getDeviceType(request.headers.get("User-Agent"));
    
        const proxyUrl = new URL(request.url);
        // Set the search param 'm' according to the device type, i.e. '0' for desktop, '1' for others
        proxyUrl.searchParams.set("m", String(deviceType === "desktop" ? 0 : 1));
    
        const proxyResponse = await fetch(proxyUrl, request);
    
        const response = new Response(proxyResponse.body, proxyResponse);
    
        // TODO: You can modify the response here :)
    
        return response;
      }
    }
    
    // Export handlers
    export default worker;
  6. Click on Save and Deploy.

Creating Routes

  1. Go to Websites section in Cloudflare Dashboard and select your domain.
  2. Now go to Workers Routes section and then click on Add Route.
  3. Input the fields as shown in the given table:
    Route Service Environment
    www.fineshopdesign.com/* remove-m-worker production
  4. *Input the fields as per your blog url and workers name.

You can route any subdomain which are hosted on Blogger to this workers in order to remove the 'm' parameter from querystring.

About the Author

~ Hello World!

Post a Comment

To avoid SPAM, all comments will be moderated before being displayed.
Don't share any personal or sensitive information.
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.