问题描述:

I am trying to redirect all my mobile users to the amp version of the website except for the home page which dont have a amp version.Every other amp page has '/amp' prefixed to the page uri path.

Below is my nginx config

if ($mobile_rewrite = perform) {

rewrite ^(?!.*(/|/amp)) http://www.example.com/amp$request_uri? permanent;

break;

}

My intention is to redirect any route to amp version except for the home page.

Redirection rule should be as:

www.example.com ---> www.example.com

www.example.com/hollywood ---> www.example.com/amp/hollywood

www.example.com/article/abc ---> www.example.com/amp/article/abc

网友答案:

You may use

^(?!/?$|(?:.*/)?amp(?:/|$))

See here what kind of input it matches.

Details:

  • ^ - start of string
  • (?!(?:/?$|(?:.*/)?amp(?:/|$))) - the negative lookahead that fails the match if the lookahead pattern matches:
    • /?$|(?:.*/)?amp(?:/|$) - either of the 2 alternatives:
      • /?$ - an optional (1 or 0 occurrences of) / and then end of string $
      • | - or
      • (?:.*/)?amp(?:/|$) - an optional sequence of any 0+ chars up to the last / (see (?:.*/)?), followed with amp that is followed with / or end of string.

To add a condition to avoid matching js or css files, add an alternative:

^(?!/?$|(?:.*/)?amp(?:/|$)|.*\.(?:js|css))
                           ^^^^^^^^^^^^^^  
相关阅读:
Top