NGINX 重定向对 URL 不起作用

NGINX 重定向对 URL 不起作用

我们最近将网站从非常旧的 Joomla 安装迁移到了 WordPress 版本。我们现在使用的是 NGINX。

Google 搜索中仍有一些页面链接到旧网站,例如:

mywebsite.com/online-shop/manufacturer/17-abc?sort=p.price&order=ASC&limit=75

我已经在 NGINX 服务器块中尝试了以下操作:

location /online-shop/manufacturer/17-abc?sort=p.price&order=ASC&limit=75  {
return 301 /shop/headsets/pagename/;
}

从技术上讲,点击 /online-shop/manufacturer/17-abc?sort=p.price&order=ASC&limit=75 后应该链接到 /shop/headsets/pagename/。

但是,我最终还是得到了 404。相同的重定向方法在常规页面上确实有效,所以我不确定“旧”url 中的特殊字符是否与此有关。

希望能得到一些专家的帮助,谢谢

PS:这是当前的 NGINX 配置。

    server {
        #listen 80 default_server;
        #listen [::]:80 default_server;
        root /var/www/html;

        index index.html index.htm index.nginx-debian.html;

        server_name www.mywebsite.com;

        location / {
            expires 90d;
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            #try_files $uri $uri/ =404;
            try_files $uri $uri/ /index.php?q=$uri&$args;
            index index.php;
        }

        location ~ \.php$ {
            try_files $uri $uri/ /index.php?$args;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

        if ( $cookie_woocommerce_items_in_cart = "1" ){
                set $skip_cache 1;
        }
        if ($request_uri ~* "(/basket.*|/cart.*|/my-account.*|/checkout.*|/addons.*|/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
                set $skip_cache 1;
        }    

        if ($http_cookie ~* "(wordpress_logged_in_|wp\-postpass_|woocommerce_items_in_cart|woocommerce_cart_hash)") {
                set $skip_cache 1;
        }

        if ($http_cookie ~* "(wc_session_cookie_[^=]*=([^%]+)%7C)") {
                set $skip_cache 1;
        }


        location ~* \.(eot|ttf|woff)$ {
            add_header Access-Control-Allow-Origin *;
        }


        location /pos  {
        return 301 /point-of-sale/website/vt;
        }


        location /online-shop/manufacturer/17-abc?sort=p.price&order=ASC&limit=75  {
        return 301 /shop/headsets/pagename/;
        }



        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/www.mywebsite.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/www.mywebsite.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    }


    server {
        listen      80;   #listen for all the HTTP requests
        server_name mywebsite.com www.mywebsite.com;
        return      301         https://www.mywebsite.com$request_uri;
    }

    server {
        if ($host = www.mywebsite.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot



        server_name www.mywebsite.com;
        listen 80;
        return 404; # managed by Certbot


    }

答案1

欢迎来到 ServerFault。

有多种方法可以匹配 url 的查询字符串,但是位置块不能同时匹配 uri 和查询字符串(它只能匹配 uri)。

问题是,您真的需要匹配“?sort=p.price&order=ASC&limit=75”吗?它看起来像是对页面元素进行排序的一种方式,有更困难的方法可以同时匹配“/online-shop/manufacturer/17-abc”和“?sort=p.price&order=ASC&limit=75”,但我认为您需要的配置是“/online-shop/manufacturer/17-abc”,其后的任何查询字符串都无关紧要(如果您只想将该 uri 与该 query_string 匹配,我会更改它)。这就是我感觉您希望它工作的方式:

location ~ ^/online-shop/manufacturer/17-abc {
    return 301 /shop/headsets/pagename/;
}

如果您需要匹配该查询字符串,只需说出来,我就会为您编写一个映射块以便读取它。

相关内容