获取干净URL的NGINX重写规则

时间:2012-01-23 05:51:35

标签: wordpress url-rewriting nginx seo

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

1 个答案:

答案 0 :(得分:2)

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

在服务器配置块中添加此代码段:

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;
        }
}