Monday, June 26, 2017

How to Disable AMP on WordPress

Few days ago I disabled AMP support on my site.

To disable AMP support, I took the following steps.
  • Log in to my WordPress dashboard
  • Go to Plugins -> Installed Plugins
  • Find "AMP" plugin
  • Click "Deactivate" link under the AMP plugin
  • Click "Delete" link under the AMP plugin
  • That is all I had to do, and 24 hours later AMP link to my site was gone from Google Search Results.

    The only issue I noticed is that old AMP links were now returning a 404 – Page Not Found message.

    For example:

    https://www.alexkras.com/my-history-with-web-development-or-javascript-fatigue/amp would return 404 since there was no longer a /amp version of the link.

    Redirect All 404s to Homepage

    To remedy this issue, I've downloaded the All 404 Redirect to Homepage

  • In WordPress dashboard
  • Go to Plugins -> Add New
  • In top right corner search for "All 404 Redirect to Homepage"
  • Click "Install Now" button for the first result
  • Click "Activate" button for the first result
  • As the name of plugin suggests, it will redirect all 404 to the home page. I already have a list of all post on my homepage rendered, via Clean My Archives plugin, so it was a decent solution in and of itself.

    Redirect All AMP links to non AMP version

    I wanted to take it one step further, though, so I modified the plugin to re-write AMP links to non amp version.

    The steps to do so were as follows:

  • In WordPress dashboard
  • Go to Plugins -> Editor
  • In top right drop down select "All 404 Redirect to Homepage" and click select
  • It should open the all-404-redirect-to-homepage/all-404-redirect-to-homepage.php file for editing
  • In it find if($options['p404_status']=='1' & $options['p404_redirect_to']!=''){ line
  • Copy paste the following code right bellow it (and indent it one tab extra so it looks readable)
  • Click "Update File" link right at the top left corner
  • if (strpos($link, "/amp") !== false) { header ('HTTP/1.1 301 Moved Permanently'); header ("Location: " . str_replace('/amp', '', $link)); exit(); }

    if (strpos($link, "/amp") !== false) {

        header ('HTTP/1.1 301 Moved Permanently');

        header ("Location: " . str_replace('/amp', '', $link));

        exit();

    }

    It should look as the following image:

    Finally, test it by navigating to an /amp version of any page on your site (after you've disabled AMP) to confirm that the redirect worked.

    I am planning to write a proper WordPress plugin and publish it on Github, if there is interest. Please follow me on Twitter if you would like to hear more or DM me if you'd like to participate.

    For now I wanted to share this quick hack as a stop-gap solution, until the proper plugin can be developed.


    Source: How to Disable AMP on WordPress

    No comments:

    Post a Comment