【问题标题】:Removing question mark from query string从查询字符串中删除问号
【发布时间】:2015-07-23 13:13:45
【问题描述】:

我的目标:将 URL 从 host.com/?abc 重写为 host.com/abc 并能够通过 $_SERVER['QUERY_STRING'] 请求它。我发现了以下规则:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /?$1 [L,QSA]

但无论出于何种原因,我的 $_SERVER['QUERY_STRING'] 都是空的,除非我输入 '?'在查询字符串前面。当我执行 print_r( $_SERVER ) 或 print_r( $_GET ) 时,我的查询字符串不存在于任何地方。有什么建议我做错了吗?谢谢!

更新; 我使用 nginx 作为“前端”代理,并使用以下规则将请求重定向到 apache:

 server
 {
  listen   80;

  root /var/hosts/hostcom;
  index index.php index.html index.htm;

  server_name host.com;

  location / {
    try_files $uri $uri/ /index.php;
  }

  location ~ \.php$ {

          proxy_set_header X-Real-IP        $remote_addr;
          proxy_set_header X-Forwarded-For  $remote_addr;
          proxy_set_header Host $host;
          proxy_pass http://127.0.0.1:8010;

   }

   location ~ /\.ht {
          deny all;
  }
 }

如果我直接请求apache端口,它可以工作host.com:8010/?abc,但不能通过nginx,有什么建议吗?谢谢!

【问题讨论】:

  • 这取决于路线,但host.com/abc 应该把“abc”放在$_GET...
  • @Random 这就是问题所在,你知道为什么没有吗?

标签: php apache mod-rewrite nginx


【解决方案1】:

来自nginx docs

如果没有找到任何文件,则内部重定向到 uri 在最后一个参数中指定。

try_files 将对不存在文件的任何请求重写为仅index.php,在到达 Apache 之前丢失了路径。

我相信您的位置块应该看起来更像这样,它应该将任何对丢失文件的请求转发给 Apache 而无需重写它:

  location / {
    try_files $uri $uri/ @apache;
  }

  location @apache {
          proxy_set_header X-Real-IP        $remote_addr;
          proxy_set_header X-Forwarded-For  $remote_addr;
          proxy_set_header Host $host;
          proxy_pass http://127.0.0.1:8010;
   }

【讨论】:

    猜你喜欢
    • 2021-11-02
    • 2015-06-25
    • 1970-01-01
    • 2015-11-28
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多