How to Redirect AMP Page to Non-AMP in Nginx, Apache, Cloudflare

Image

 Accelerated Mobile Pages also called AMP is an open-source framework for web components developed by Google that you can use to create user-friendly websites email, stories ads, and more.

It's basically the bare-bones variant of an HTML page that is designed to be extremely light and fast to load. The system is built for speed and readability, as well as high-speed caching on mobile phones. As in a recent announcement by Google they're taking away the AMP requirement for Top Storie's eligibility.

If you are planning to unsubscribe from AMP you'll need to turn off this AMP plugin (if you're using WordPress). The issue is that search engines such as Google continue to point to your original page by adding the suffix /amp and may send your visitors to the page 404: Error Page Not Found.

So, what's the next step after you have deactivated AMP to avoid these mistakes?

Easy!

You must properly redirect your pages after deactivating AMP to avoid this situation.

If you're interested in redirecting your AMP site to another non-AMP version is the best spot as we'll discuss ways to accomplish this in the most popular websites and applications.

Let's start.

Nginx

The configuration for Nginx is saved in nginx.conf.

The file should be available in /etc/nginx or /etc/nginx/conf.d on Linux servers (for an open-source version of NGINX, the location may differ like /usr/local/nginx/conf or /usr/local/etc/nginx).

Make changes to the nginx.conf file where your domain's configuration is stored. This is where you must enable AMP to non-AMP redirection. Add below the rewrite line within the {server server's block:

Rewrite ^/(. *)\/amp http://example.com/$1 permanent;

Replacing example.com using your personal domain, and keep the document. Restart the nginx server for the redirection to be modification efficient.

$ sudo systemctl restart nginx

You can now check whether redirection is functioning from your browser.

Apache

Apache uses .htaccess directives. You can modify the .htaccess file located in the domain root. Add the following lines to add AMP to the redirection that is not AMP:

RewriteEngine On
RewriteCond % (.+)/amp(. *)$
RewriteRule ^ %1/ [R=301,L]

Save the modifications to .htaccess file and check redirection in your browser.

The code above is one you could use with an open-source hosting platform for WordPress or any other CMS. If you're not sure about .htaccess modification, inquire with your hosting company.

Cloudflare

It's much easier if you use Cloudflare. It is possible to use Rules that are located in the top bar of icons following your login.

You could have a similar URL like the one below.

https://example.com/*/amp/

Setup as Forwarding URL using the URL 301 and destination as shown below.

https://syntaxfix.com/$1

Click on Save and Deploy

Take a few seconds, then try accessing the AMP URL. You'll be able to see it redirecting to a page that is not AMP-related.

Summary

We've provided a brief overview on AMP and the reasons it's been around. AMP to redirection to non-AMP is an essential procedure to perform after the deactivation of AMP to prevent losing visitors to your website because of errors with 404. This is possible with permanent redirection like the one mentioned in the previous paragraph.