【问题标题】:Updating Old Wordpress Permalinks on Nginx在 Nginx 上更新旧的 Wordpress 永久链接
【发布时间】:2012-08-19 19:56:34
【问题描述】:

我正在尝试将我的网站从默认的 /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;
                            }
  }

有人知道怎么解决吗?谢谢。

【问题讨论】:

  • 我也试过添加重写^/[0-9]{4}/[0-9]{2}/[0-9]{2}/([a-z0- 9\-/]+) website.com/$1 永久;但是在位置 / {} 中不起作用。

标签: regex wordpress mod-rewrite nginx


【解决方案1】:

修复很简单:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-04-19
      • 2021-03-28
      • 2016-10-04
      • 2013-03-26
      • 1970-01-01
      • 2015-02-20
      • 2017-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多