AMP Rewrite problem on NGINX server

May 25, 2017 4.2k views
Ubuntu 16.04 Nginx WordPress PHP Frameworks

I have a Wordpress site that is currently serving AMP pages to mobile visitors.

I’d like to redirect all AMP pages (i.e. ) to no AMP pages (i.e. ) in order to be able to remove the AMP pluing without loosing traffic.

For some reason whatever I try to modify my config file nothing changes.

My curreny configuration is as follow:

server {
listen 80 defaultserver;
listen [::]:80 default
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
location / {
rewrite ^(.)$ /$1$2 redirect;
rewrite ^(.
/)amp/$ $1 permanent;
try_files $uri $uri/ =404;

What i’m doing wrong?
Thank you

2 Answers


For WordPress, instead of:

try_files $uri $uri/ =404;

… you’d generally want to use:

try_files $uri $uri/ /index.php?$args;

When you have the =404 at the end, you’re essentially telling NGINX that if a directory or file does not exist, throw a 404 error. Since WordPress uses permalinks, the vast majority of the URL’s are not going to exist as files or directories, so the end result is a 404.

For example:

If /category/ doesn’t exist as a directory or file, it’s a 404. If /category/ does exist as a directory but /this-really-cool-post doesn’t exist as a directory or file, it’s a 404.

As for the rewrite, you might be able to try something like:

rewrite ^/amp(.*) http://$server_name/$1 permanent;

Thanks for the clarification.
I have tried also this rewrite option, but it’s still not working: amp pages keep showing up as before
I have refreshed caches, of course.

Have another answer? Share your knowledge.