gpt4 book ai didi

url-rewriting - Nginx 将 url 参数重写为子域

转载 作者:行者123 更新时间:2023-12-04 05:33:35 25 4
gpt4 key购买 nike

有许多我希望重定向到子域的不同 url 参数。

mydomain.com/blog/title-of-the-post -> blog.mydomain.com/title-of-the-post



mydomain-com/taxonomy/term/id -> mydomain.com/id

所以逻辑几乎是重写,但想要获取和使用一些 url。这能做到吗?

最佳答案

是的,您可以在重写中使用反向引用,例如:

location /blog/ {
rewrite ^/blog/(.*)$ http://blog.mydomain.com/$1 permanent;
}

同样的事情适用于您的其他重写

更新:

如果你想保留博客部分,你不再改变 $uri,只改变 $host,然后变得更容易:
location /blog/ {
rewrite ^ http://blog.mydomain.com/$uri permanent;
}

注意:nginx 标准变量列于 http://wiki.nginx.org/HttpCoreModule#Variables

对于多个参数,您使用进一步的反向引用 $2 $3 等等(只需确保在要在正则表达式中使用的部分周围有分组,即括号),例如:
location /blog/ {
rewrite ^/blog/([0-9]{4})/(.*)$ http://blog.mydomain.com/$1/$2 permanent;
}

这将匹配您博客网址中的 4 位数年份,例如参见 http://www.regular-expressions.info/brackets.html有关正则表达式和反向引用的更多信息

关于url-rewriting - Nginx 将 url 参数重写为子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12270733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com