Report this

What is the reason for this report?

AMP Rewrite problem on NGINX server

Posted on May 25, 2017

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

I’d like to redirect all AMP pages (i.e. https://mysite.com/page/amp/ ) to no AMP pages (i.e. https://mysite.com/page/ ) 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



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!

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.

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.

@infoa6a708ecd7817cd8c24379

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:

domain.com/category/this-really-cool-post/

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;

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.