Rewrite directory as parameter

梦想的初衷 提交于 2019-12-14 02:06:09

问题


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:


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



来源:https://stackoverflow.com/questions/44604909/rewrite-directory-as-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!