【问题标题】:Translated url path to query params for nginx用于查询 nginx 参数的翻译 url 路径
【发布时间】:2017-04-02 16:32:48
【问题描述】:

网址https://example.com/profile/chico nginx必须理解为: https://example.com/profile/?username=chico(当前有效的网址)。

https://example.com/profile/ 目前也可以正常工作,因为它目前解析为 index.php,没有用户名参数。

我现在有

location /profile/ {
    index /profile/index.php;
}

网络上有很多不同的东西,我没有找到我需要的具体内容。

也试过了

location /profile/ {
    rewrite ^/profile/(.*)$ /profile/?username=$1;
}

【问题讨论】:

    标签: .htaccess nginx


    【解决方案1】:

    试试:

    location /profile/ {
        try_files $uri @rewrite;
    }
    location @rewrite {
        rewrite ^/profile/(.+)$ /profile/index.php?username=$1 last;
        rewrite ^ /profile/index.php last;
    }
    

    如果第一个 rewrite 匹配,则忽略第二个 rewrite。使用 .php 扩展名重写 URI 后,处理将移至不同的 location 块。

    请参阅this document 了解更多信息。

    编辑:添加了try_files 块来处理静态内容。

    【讨论】:

    • 谢谢,离工作更近了,实际加载了带有用户名 var 的页面,但页面上的资产导致 404(例如 /profile/assets/js/main.js 不能找到,对于图像和 css 文件也是如此)
    • 好吧,没有意识到你在那个位置有静态内容。答案已更新。
    • 太棒了。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 2020-09-14
    • 2019-11-12
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多