Nginx重写/ product_category / WordPress

我正在尝试为WordPress / Woocommerce网站重写以下内容:

/product_category/example-category/ /product/example-product/ 

至…

 /example-category/ /example-product/ 

使用以下规则:

 server { listen 10.99.0.3:8080; server_name www.example.com; root /home/www.example.com/public_html; index index.html index.htm index.php; rewrite ^/product-category/1$ /; rewrite ^/product/1$ /; include conf.d/whitelisted.conf; include conf.d/wp/restrictions.conf; include conf.d/wp/wordpress.conf; } 

…这是从一个单独的文件包含的wordpress.conf规则:

 location / { try_files $uri $uri/ /index.php?$args; } rewrite /wp-admin$ $scheme://$host$uri/ permanent; location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ { access_log off; log_not_found off; expires max; } location ~* /(?:uploads|files)/.*\.php$ { deny all; } location ~* /wp-content/.*\.php$ { deny all; } location ~* /wp-includes/.*\.php$ { deny all; } location ~* /(?:uploads|files|wp-content|wp-includes)/.*\.php$ { deny all; } location ~ [^/]\.php(/|$) { fastcgi_split_path_info ^(.+?\.php)(/.*)$; if (!-f $document_root$fastcgi_script_name) { return 404; } include fastcgi_params; fastcgi_pass unix:/var/run/php-fpm/php5-fpm.sock; fastcgi_index index.php; include /etc/nginx/fastcgi_params; fastcgi_buffer_size 128k; fastcgi_buffers 256 16k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; fastcgi_read_timeout 1800; } 

但Nginx似乎忽略了我为产品猫/产品重写的重写规则,就好像它们不存在一样。 例如,如果我访问:

 http://www.example.com/product-category/footwear/ 

而不是重写为:

 http://www.example.com/footwear/ 

它只是提供:

 http://www.example.com/product-category/footwear/ 

我究竟做错了什么? 谢谢!

您的重写使用正则expression式,有趣的是,它们被设置为只匹配特定的URL。

 rewrite ^/product-category/1$ /; rewrite ^/product/1$ /; 

所以,只有URLs /product-category/1/product/1才能匹配这些指令。 无论是/product/2还是/product/air-jordan-1-retro-high-og-banned-2016-release都不会匹配。

我想你想要做的是捕获剩余的URL,并在目标url中使用。

 rewrite ^/product-category/(.*) /$1; rewrite ^/product/(.*) /$1; 

但是等等,还有更多! 您尚未为rewrite指令指定可选标志。 所以,在重写URL之后,nginx继续通过configuration的快乐方式。 它不会开始处理请求或redirect用户代理。 这可能会导致WordPress感到困惑。 如果你想redirect(例如search引擎优化),那么你应该添加适当的标志。

 rewrite ^/product-category/(.*) /$1 permanent; rewrite ^/product/(.*) /$1 permanent;