0

我正在尝试将我的网站从默认的 /year/month/day/post_title 永久链接更改为简单的 /post_title/ 链接,但是,在更改它时,我所有的旧链接都已损坏。我已经阅读了一个关于如何使用 .htaccess 在 Apache 上执行此操作的站点,但需要一些帮助来弄清楚如何使其与 nginx 的位置而不是 mod_rewrite 一起工作。
这是详细说明如何在 Apache 上执行此操作的站点http://www.rickbeckman.org/how-to-update-your-wordpress-permalinks-without-causing-link-rot/
我尝试使用这个 htaccess 到 nginx转换器http://winginx.com/htaccess但是,正则表达式可能会导致问题,并且在启动 nginx 时出现此错误

    [emerg]: unknown directive "4}/[0-9]" in /usr/local/nginx/sites-enabled/website.com:19

这是我的配置文件

    server {
        listen   80;
        server_name  website.com;
        rewrite ^/(.*) http://www.website.com/$1 permanent;
   }

    server {
        listen   80;
        server_name www.website.com;
        error_log /home/user/public_html/website.com/log/error.log;
        client_max_body_size 10M;
        client_body_buffer_size 128k;

        location /  {
                    root   /home/user/public_html/website.com/public/;
                    index  index.php index.html;
                    try_files $uri $uri/ /index.php;
                    }
        location ~ ^/[0-9]{4}/[0-9]{2}/[0-9]{2}/([a-z0-9\-/]+) {
                    rewrite ^(.*)$ http://website.com/$1 permanent;
                    }
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$
                            {
        fastcgi_read_timeout 120;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include /usr/local/nginx/conf/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /home/user/public_html/website.com/public/$fastcgi_script_name;
                            }
  }

有人知道如何解决吗?谢谢。

4

2 回答 2

0

修复很简单:

location ~ ^/[0-9]{4}/[0-9]{2}/[0-9]{2}/([a-z0-9\-/]+) {
  rewrite ^(.*)$ http://website.com/$1 permanent;
}

上面的问题是重写重置了反向引用,因此 $1 现在与重写匹配中的整个 url 匹配,而不是位置匹配中的 $1。以下应该有效:

location ~ ^/[0-9]{4}/[0-9]{2}/[0-9]{2}/([a-z0-9\-/]+) {
  set $post_title $1;
  rewrite ^(.*)$ http://website.com/$post_title permanent;
}

(或者再次匹配重写规则中的正则表达式)

于 2012-08-19T20:48:22.920 回答
0

通过在正则表达式周围添加引号来修复它。

    rewrite "^/[0-9]{4}/[0-9]{2}/[0-9]{2}/([a-z0-9\-/]+)" http://www.website.com/$1;
于 2012-08-20T05:17:12.510 回答