A
A
Andrey2017-01-31 19:45:46
Nginx
Andrey, 2017-01-31 19:45:46

How to properly rewrite in Nginx?

Hello super community.
Tell me please.
I want to make it so that users accessing the site from a smartphone
see AMP pages, but at the same time, if they go to the main page, then it is given without the /amp prefix.
That is, you need this:
examle.ru -> examle.ru
but
examle.ru /post-1/ -> examle.ru/post-1/amp
examle.ru/page-3/ -> examle.ru/page-3/amp
Now in nginx like this:

if ($mobile_rewrite = perform) {
        rewrite ^(.*(?<!/amp))/$ https://$server_name$1/amp/ break;
}

and everyone who sees from smartphones like this:
examle.ru -> examle.ru/amp
examle.ru/post-1/ -> examle.ru/post-1/amp
examle.ru/page-3/ -> examle.ru /page-3/amp
How to fix?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
ky0, 2017-01-31
@ky0

Make two locations - with exact match and prefix, get rid of regular expressions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question