首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NGINX重定向不适用于URL

NGINX重定向不适用于URL
EN

Server Fault用户
提问于 2019-01-18 09:52:19
回答 1查看 1.2K关注 0票数 0

我们最近从一个非常古老的Joomla安装转移到我们网站的WordPress版本。我们现在用的是NGINX。

谷歌搜索中仍有几个页面,链接到旧网站,如:

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

我在我的NGINX服务器块中尝试了以下几种方法:

代码语言:javascript
运行
复制
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/耳机/页面名/如果点击了。

然而,我最终还是得到了404。同样的重定向方法也适用于常规页面,因此我不确定“旧”url中的特殊字符是否与此有关。

如能提供专家帮助,将不胜感激。

PS:这里是目前的NGINX conf。

代码语言:javascript
运行
复制
    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


    }
EN

回答 1

Server Fault用户

发布于 2019-01-18 14:39:46

欢迎来到ServerFault。

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

问题是,你真的需要匹配"?sort=p.price&order=ASC&limit=75“吗?这看起来像是对页面元素进行排序的一种方式,同时匹配“/online-shop/ page /17-abc”和"?sort=p.price&order=ASC&limit=75“的方法要困难得多,但我认为您需要的配置是”/online/page/17-abc“和任何后面的查询字符串--这都不重要(如果您只想将那个uri与那个query_string匹配的话,我会修改它)。这就是我觉得你想让它发挥作用的方式:

代码语言:javascript
运行
复制
location ~ ^/online-shop/manufacturer/17-abc {
    return 301 /shop/headsets/pagename/;
}

如果您需要匹配该查询字符串,只需说出来,我将编写一个映射块供您读取。

票数 0
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/949677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档