将目录重写为参数 [英] Rewrite directory as parameter
问题描述
我正在尝试从中重定向请求
I'm trying to redirect requests from
example.com/abc234
至
example.com/setup.html?s=abc234
到目前为止,我已经尝试了以下方法,但它似乎总是以1)不传输参数或2)以无限循环(或404)结尾而结束,因为它还会尝试重定向已重定向的请求?该请求必须明显被重写,因为我想使用JS而不是PHP来获取参数.
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;
}
我还尝试了location / { try_files ...; }
的各种组合,或者在rewrite
中使用绝对URL都没有成功.
I've also tried various combinations of location / { try_files ...; }
or using the absolute URL within rewrite
without success.
推荐答案
一种技术是仅重写与物理文件不匹配的URI.
One technique is to rewrite only URIs that do not match a physical file.
例如:
server {
server_name example.com;
root /var/www/html;
location / {
try_files $uri $uri/ @rewrite;
}
location @rewrite {
return 302 /setup.html?s=$uri;
}
}
有关更多信息,请参见此文档. 重写^(.*)$/setup.html?s=$1重定向; }
See this document for more. rewrite ^(.*)$ /setup.html?s=$1 redirect; }
这篇关于将目录重写为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!