AMP Rewrite problem on NGINX server

May 25, 2017 675 views
Nginx WordPress PHP Frameworks Ubuntu 16.04

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 defaultserver;
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

2 Answers

@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;

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.