wordpress - 用于获取干净 URL 的 NGINX 重写规则

标签 wordpress url-rewriting nginx seo

将我的 wordpress 永久链接结构从 /%category%/%postname%/ 重定向到 /%postname%/ 的 nginx 重写规则是什么?

最佳答案

总而言之,您需要让 NGINX 知道如果该文件不存在,不要抛出 404 错误,而是调用 index.php。 Wordpress 足够聪明,可以将 URL 解析为参数,并提供正确的页面。

在您的服务器配置 block 中添加此代码段:

location / {
    try_files   $uri $uri/ /index.php?$args;
}

这是来自 nginx.org 的完整示例:

# Upstream to abstract backend connection(s) for php
upstream php {
        server unix:/tmp/php-cgi.socket;
        server 127.0.0.1:9000;
}

server {
        ## Your website name goes here.
        server_name domain.tld;
        ## Your only path reference.
        root /var/www/wordpress;
        ## This should be in your http block and if it is, it's not needed here.
        index index.php;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location / {
                # This is cool because no php is touched for static content.
                # include the "?$args" part so non-default permalinks doesn't break when using query string
                try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include fastcgi.conf;
                fastcgi_intercept_errors on;
                fastcgi_pass php;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }
}

关于wordpress - 用于获取干净 URL 的 NGINX 重写规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8967682/

相关文章:

iis - 将子目录重定向到 HTTPS

nginx - 使用 proxy_cache 的 nginx 中的缓存不起作用。

ssl - nginx 和 bitnamis dockerized osclass 的 HTTP/HTTPS 重定向问题

javascript - WordPress 循环内循环错误

影响 WordPress 菜单的 jQuery slider

php - 适用于 WordPress 的 Cat + 标签过滤器插件 : Checkboxes not being saved

url - 错误域的隐私错误

IIS Url Rewrite 覆盖外部 url

nginx - PHP-FPM 和 Nginx 重写导致下载

css - 为什么我不能在 Wordpress 中正确设置 block 引用的样式?