【问题标题】:nginx PHP User Profile Rewritingnginx PHP 用户配置文件重写
【发布时间】:2014-12-11 08:15:15
【问题描述】:

我正在尝试将我的 url 配置文件 URL 重写为干净且不包含参数,至少对用户而言。我正在使用 nginx 服务器,但无法正常工作。

网站示例: http://website.com/subfolder/

个人资料链接: http://website.com/subfolder/profile.php

什么有效: http://website.com/subfolder/profile.php?username=Bobby

我想做什么: http://website.com/subfolder/Bobby

我的网站配置中的代码块:

location /subfolder/ {
    if ($query_string ~ "^username=([A-Za-z0-9]+)$"){
        rewrite ^/profile.php$ http://website.com/subfolder/%1 redirect;
    }
}

【问题讨论】:

  • 只是一个提示,您正在做相反的事情,您尝试在内部将带有查询字符串的请求重写为 PHP 无法理解的内容。您需要在 PHP 代码中编写最终 URL,并在 Nginx 内部管理从这个新 url 到查询字符串版本的重写。

标签: php nginx url-rewriting


【解决方案1】:

您的重定向几乎是正确的。只是外观上的小改动。

location = /subfolder/profile.php {
    if ($arg_username) {
        return 301 /subfolder/$arg_username/;
    }
    fastcgi_pass 127.0.0.1:9001;
    include fastcgi_params;
}

现在,regilero 告诉你,你需要在脚本中解析 /subfolder/Username/ 或者你可以通过 nginx 来完成

location ~ ^/subfolder/(?<username>.+)/$ {
   rewrite ^(.*)$ /subfolder/profile.php?username=$username break;
   fastcgi_pass 127.0.0.1:9001;
   include fastcgi_params;
   fastcgi_param REQUEST_URI $uri$is_args$args;
}

2 重要注意事项:重写 break 关键字以防止重定向循环; fastcgi_param REQUEST_URI 通常是$request_uri(在 fastcgi_params 中),但它是原始 URI,所以我们需要更改它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-04
    • 2013-01-12
    • 1970-01-01
    • 2019-01-08
    • 2016-01-31
    • 1970-01-01
    • 2018-12-08
    • 2019-11-07
    相关资源
    最近更新 更多