【问题标题】:Nginx rewrite. PHP GET Parameters have part of url when in sub-directoryNginx 重写。 PHP GET 参数在子目录中时包含部分 url
【发布时间】:2017-11-14 13:48:10
【问题描述】:

所以,我的目录结构是这样的;

index.php

location / {
        rewrite ^(.*)$ /index.php?params=$1 last;
    }

公共/资产/

    location /public/assets {
         rewrite ^(.*)$ /public/assets/index.php?params=$1 last;
    }

所以,我确实需要对这个项目进行 2 次重写,一次用于期望资产的所有内容,一次用于资产。但是使用 Nginx 时出现问题。使用 Nginx 重写 public/assets PHP $_GET 参数如下:

Array
(
    [params] => /public/assets/css/syles.css
)
Array
(
    [0] => 
    [1] => public
    [2] => assets
    [3] => css
    [4] => syles.css
)

这些都是错误的。我不希望 url 传递给 GET。因为在使用 Apache 时,我确实得到了以下结果。使用 Apache 时,代码可以正常工作。

Array
(
    [params] => css/styles.css
)
Array
(
    [0] => css
    [1] => styles.css
)

那么,如何使用 Nginx 配置从 url 中删除这些“额外”参数?或者如果网络服务器是 Nginx,我是否需要使用 PHP 来删除这个额外的参数?我什至不希望将该解决方案作为解决方案提出。

所以,我希望使用 Nginx 获得与使用 Aoache 获得的参数相同的参数。


在使用 Apache 时仅供参考,我将以下 .htaccess 放在 public/assets 下。

RewriteEngine on
RewriteCond %{REQUEST_URI} !/index.php
RewriteRule ^(.*)$ index.php/?params=$1 [NC] #L

【问题讨论】:

    标签: php apache .htaccess nginx url-rewriting


    【解决方案1】:

    重写之前的路径包含/public/assets/,因此将其从传递给params 的内容中排除的最简单方法是在正则表达式中对其进行硬编码,并仅提取您想要的内容,使用类似:

    location /public/assets {
             rewrite ^/public/assets/(.*)$ /public/assets/index.php?params=$1 last;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-18
      • 2021-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多