无查询参数的NGINX严格重定向
我有一个网址:无查询参数的NGINX严格重定向,nginx,redirect,Nginx,Redirect,我有一个网址: https://www.example.com/test.html 此URL可以具有如下查询参数: https://www.example.com/test.html?cat=11&rating=5 我只想重定向: https://www.example.com/test.html 致: 当我这样做时,通过: location = /test.html { return 301 https://example.com/test2; } 带有参数的URL还重
https://www.example.com/test.html
此URL可以具有如下查询参数:
https://www.example.com/test.html?cat=11&rating=5
我只想重定向:
https://www.example.com/test.html
致:
当我这样做时,通过:
location = /test.html {
return 301 https://example.com/test2;
}
带有参数的URL还重定向:
重定向到:
是否可以在不影响参数的情况下严格重定向URL
更新:我仍然希望正常工作,但是应该重定向到/test2
更新:NGINX的我的本地docker机器服务器部分
server {
listen 80;
listen [::]:80;
server_name magento.test;
set $MAGE_ROOT /var/www/php;
set $maintenance off;
if (-f $MAGE_ROOT/maintenance.enable){
set $maintenance on;
}
include /var/www/php/nginx.conf;
}
更新:
root $MAGE_ROOT;
index index.php index.html index.htm;
autoindex off;
charset UTF-8;
error_page 404 403 = /errors/404.php;
#add_header "X-UA-Compatible" "IE=Edge";
add_header 'X-Content-Type-Options' 'nosniff';
# Deny access to sensitive files
location /.user.ini {
deny all;
}
# PHP entry point for setup application
location ~* ^/setup($|/) {
root $MAGE_ROOT;
location ~ ^/setup/index.php {
fastcgi_pass php:9000;
fastcgi_param PHP_FLAG "session.auto_start=off \n suhosin.session.cryptua=off";
fastcgi_param PHP_VALUE "memory_limit=4000M \n max_execution_time=600";
fastcgi_read_timeout 600s;
fastcgi_connect_timeout 600s;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ ^/setup/(?!pub/). {
deny all;
}
location ~ ^/setup/pub/ {
add_header X-Frame-Options "SAMEORIGIN";
}
}
# PHP entry point for update application
location ~* ^/update($|/) {
root $MAGE_ROOT;
location ~ ^/update/index.php {
fastcgi_split_path_info ^(/update/index.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
# Deny everything but index.php
location ~ ^/update/(?!pub/). {
deny all;
}
location ~ ^/update/pub/ {
add_header X-Frame-Options "SAMEORIGIN";
}
}
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /pub/ {
location ~ ^/pub/media/(downloadable|customer|import|custom_options|theme_customization/.*\.xml) {
deny all;
}
alias $MAGE_ROOT/pub/;
add_header X-Frame-Options "SAMEORIGIN";
}
location /static/ {
# Uncomment the following line in production mode
# expires max;
# Remove signature of the static files that is used to overcome the browser cache
location ~ ^/static/version {
rewrite ^/static/(version\d*/)?(.*)$ /static/$2 last;
}
location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2|html|json)$ {
add_header Cache-Control "public";
add_header X-Frame-Options "SAMEORIGIN";
expires +1y;
if (!-f $request_filename) {
rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
}
}
location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
add_header Cache-Control "no-store";
add_header X-Frame-Options "SAMEORIGIN";
expires off;
if (!-f $request_filename) {
rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
}
}
if (!-f $request_filename) {
rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
}
add_header X-Frame-Options "SAMEORIGIN";
}
location /media/ {
try_files $uri $uri/ /get.php$is_args$args;
location ~ ^/media/theme_customization/.*\.xml {
deny all;
}
location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
add_header Cache-Control "public";
add_header X-Frame-Options "SAMEORIGIN";
expires +1y;
try_files $uri $uri/ /get.php$is_args$args;
}
location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
add_header Cache-Control "no-store";
add_header X-Frame-Options "SAMEORIGIN";
expires off;
try_files $uri $uri/ /get.php$is_args$args;
}
add_header X-Frame-Options "SAMEORIGIN";
}
location /media/customer/ {
deny all;
}
location /media/downloadable/ {
deny all;
}
location /media/import/ {
deny all;
}
location /media/custom_options/ {
deny all;
}
location /errors/ {
location ~* \.xml$ {
deny all;
}
}
# PHP entry point for main application
location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check)\.php$ {
try_files $uri =404;
fastcgi_pass php:9000;
fastcgi_buffers 1024 4k;
fastcgi_param PHP_FLAG "session.auto_start=off \n suhosin.session.cryptua=off";
fastcgi_param PHP_VALUE "memory_limit=4000M \n max_execution_time=18000";
fastcgi_read_timeout 600s;
fastcgi_connect_timeout 600s;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
gzip on;
gzip_disable "msie6";
gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types
text/plain
text/css
text/js
text/xml
text/javascript
application/javascript
application/x-javascript
application/json
application/xml
application/xml+rss
image/svg+xml;
gzip_vary on;
# Banned locations (only reached if the earlier PHP entry point regexes don't match)
location ~* (\.php$|\.phtml$|\.htaccess$|\.git) {
deny all;
}
查询参数不受
location
或rewrite
指令中的测试,它们仅适用于HTTP请求的规范化URI。执行所需操作的唯一方法是检查(或)变量:
location=/test.html{
如果($is_args=''){
返回301/test2;
}
…#默认请求处理
}
如果您显示完整的服务器
块,也许我可以建议更优化的解决方案。例如,如果您只有一个位置
块,则类似的操作应该可以:
位置/{
如果($is_args=''){
重写^/test\.html$/test2永久;
}
…#默认请求处理
}
但是,此配置可能有一个警告。我不知道不同的浏览器在缓存重定向时是否考虑了查询字符串,这可能会导致无条件重定向,任何对/test.html
的请求都会导致无条件重定向,无论查询字符串是否存在。为了避免这种情况,我建议使用302临时重定向:
return 302 /test2;
或
更新
您可以将此检查添加到主配置文件中的服务器上下文中:
服务器{
听80;
听[:]:80;
服务器名称magento.test;
设置$MAGE_ROOT/var/www/php;
抵销$maintenance;
如果(-f$MAGE_ROOT/maintenance.enable){
设置$maintenance;
}
如果($is_args=''){
重写^/test\.html$/test2永久;
}
包括/var/www/php/nginx.conf;
}
或到magento配置文件的location/{…}
块:
位置/{
如果($is_args=''){
重写^/test\.html$/test2永久;
}
try_files$uri$uri//index.php$is_args$args;
}
@KalvinKlien 1$args
变量值不包含问号,右指令是返回301/hardware.html?$args代码>。2.你确定这不是缓存的结果吗?您能从命令行用类似于curl
的东西检查实际的服务器响应吗?不管怎样,我要在我的沙盒上检查一下,我会告诉你测试后得到了什么。@KalvinKlien哦,当然你在这个配置中得到了一个无限循环,因为你正在将/hardware.html?一些查询字符串
重定向到它自己。我的意思是在浏览器内部缓存(它们像任何其他静态内容一样缓存301重定向),而不是nginx缓存。您需要像处理任何其他请求一样处理此/hardware.html
请求,但我需要查看整个服务器
块,以建议如何处理。@KalvinKlien再次-位置
和重写
指令都与HTTP请求的标准化URI一起工作,而不是整个请求字符串。您不能使用位置
或重写
指令检查查询字符串,标准化URI不包含该字符串,在您的情况下,它只包含/hardware.html
,无论查询字符串是否存在。您不需要在nginx正则表达式中屏蔽/
符号(尽管您可以这样做,如果您愿意的话),但是如果您不屏蔽点符号,它将匹配任何字符。@KalvinKlien我需要查看您的位置
块,这个块什么都没有给我。它们可能在/var/www/php/nginx.conf
中?看看我的第二个例子,我使用的是location/{…}
,而不是/location=test.html{…}
。这是因为我想以默认方式处理任何其他请求,包括带有查询参数的/hardware.html
。@KalvinKlien如果($is_args=''){rewrite^/hardware\.html$/test page permanent;}
直接在服务器
上下文中,在任何位置
块之外,将其添加到include/var/www/php/nginx.conf之前代码>行。
return 302 /test2;
rewrite ^/test\.html$ /test2 redirect;