1

i am using nginx web server for my wordpress website. i am going to make it to become amp version for mobile serve.

i wanna add /amp/ to my url. i also using pretty permalink for my current url,so may i know how can i rewrite my url in mobile version to /amp/

i am using rewrite ^ http://example.com$request_uri/amp/ break; but when i serve the web it become this

exmaple.com/homepage//amp//amp//amp//amp//amp//amp//amp//amp//amp//amp//amp//amp//amp//amp//amp//amp//amp//amp//amp//amp//amp/

i wish when iphone serve the webpage, the link will be http://exmaple.com/homepage/amp/

the main purpose i only want add /amp/ to any link of the website.

this is my nginx.conf

server {
listen       80;
server_name  example.com;
charset utf-8;
access_log  logs/xxx.access.log;

root   /var/www/html;


index index.php;

location / {
    try_files $uri $uri/ /index.php?q=$uri;
}

location /en {
    try_files $uri $uri/ /en/index.php?q=$uri;
}

location /my {
    try_files $uri $uri/ /my/index.php?q=$uri;
}

set $mobile_rewrite do_not_perform;
if ($http_user_agent ~* "  (android|bb\d+|meego).+mobile|ip(hone|od) {
set $mobile_rewrite perform;
}


## redirect to AMP ##
if ($mobile_rewrite = perform) {
rewrite ^ http://example.com$request_uri/amp/ break;

break;

}

Thanks

1
  • @JohnsonGoey, did the present answer work for you? If so, please accept and upvote.
    – cnst
    Jul 6, 2016 at 22:07

1 Answer 1

1

It sounds like you have a redirect loop, because you keep on adding /amp/ at the end of your request URL.

Perhaps you should have a conditional rewrite directive (or two!) instead of a wildcard.

-rewrite ^ http://example.com$request_uri/amp/ break;
+rewrite ^(.*(?<!/amp))/$ http://example.com$1/amp/ break;
+rewrite ^.*(?<!/amp/)$ http://example.com$uri/amp/ break;

The above ensures that the rewrite will only happen if your URL doesn't already end in /amp/ (using the lookbehind assertions of PCRE, which is the library that NGINX is using for support of the regular expressions); additionally, it should also take care of the double // that were appearing in your scheme originally.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .