从查询字符串中删除问号
Removing question mark from query string
我的目标:将 URL 从 host.com/?abc 重写为 host.com/abc 并能够通过 $_SERVER['QUERY_STRING'] 请求它。我发现了以下规则:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /? [L,QSA]
但无论出于何种原因,我的 $_SERVER['QUERY_STRING'] 是空的,除非我输入 '?'在查询字符串的前面。当我执行 print_r( $_SERVER ) 或 print_r( $_GET ) 时,我的查询字符串在任何地方都不存在。有什么建议我做错了吗?谢谢!
更新;
我将 nginx 用作 'front' 代理并使用以下规则将请求重定向到 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,有什么建议吗?谢谢!
来自nginx docs:
If none of the files were found, an internal redirect to the uri
specified in the last parameter is made.
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;
}
我的目标:将 URL 从 host.com/?abc 重写为 host.com/abc 并能够通过 $_SERVER['QUERY_STRING'] 请求它。我发现了以下规则:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /? [L,QSA]
但无论出于何种原因,我的 $_SERVER['QUERY_STRING'] 是空的,除非我输入 '?'在查询字符串的前面。当我执行 print_r( $_SERVER ) 或 print_r( $_GET ) 时,我的查询字符串在任何地方都不存在。有什么建议我做错了吗?谢谢!
更新; 我将 nginx 用作 'front' 代理并使用以下规则将请求重定向到 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,有什么建议吗?谢谢!
来自nginx docs:
If none of the files were found, an internal redirect to the uri specified in the last parameter is made.
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;
}