AMP Rewrite problem on NGINX server

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 default_server; listen [::]:80 default_server; 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

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.


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.