Rewrite directory as parameter

2019-07-14 04:55发布

I'm trying to redirect requests from

example.com/abc234 to

example.com/setup.html?s=abc234

So far, I've tried the following, but it seems to always end up either 1) not transmitting the parameter or 2) ending up in an infinite loop (or 404) because it also tries to redirect the redirected request? The request has to be visibly rewritten because I want to pick up the parameter with JS, not PHP.

server {
 server_name  example.com;
 root         /var/www/html;
 rewrite      ^(.*)$ /setup.html?s=$1 redirect;
}

I've also tried various combinations of location / { try_files ...; } or using the absolute URL within rewrite without success.

1条回答
该账号已被封号
2楼-- · 2019-07-14 05:29

One technique is to rewrite only URIs that do not match a physical file.

For example:

server {
    server_name  example.com;
    root         /var/www/html;

    location / {
        try_files $uri $uri/ @rewrite;
    }
    location @rewrite {
        return 302 /setup.html?s=$uri;
    }
}

See this document for more. rewrite ^(.*)$ /setup.html?s=$1 redirect; }

查看更多
登录 后发表回答