WP& Nginx:重定向包含' / folder /'的帖子网址但不是包含' / folder /'的图片网址

时间:2014-05-15 09:23:21

标签: wordpress image redirect nginx

在Wordpress / Nginx网站上,我正在删除/重定向我们网站上的一部分文章,并希望将此内容的所有网址重定向到该网站的其他部分。

我的工作效果非常好(见下文),但不幸的是,图像文件夹的名称还包含我用来捕捉重定向的字符串。

我们希望这些图像能够显示出来。以下是我正在尝试实现的所需行为的一些示例网址:

  1. http://www.website.com/contenttoberemoved/ - 重定向到http://www.website.com/other/section/
  2. http://www.website.com/contenttoberemoved/an-article/ - 重定向到http://www.website.com/other/section/
  3. http://www.website.com/wp-content/themes/theme-name/custom/contenttoberemoved/images/image-name.jpg - 执行无重定向
  4. 这是当前的Nginx配置:

    # Addresses URL #1 above
        if ( $request_filename ~ contenttoberemoved/ ) {
            rewrite ^(.*) http://www.website.com/category/articles/ permanent;
        }
    
    # Addresses thousands of URLs with the structure of #2 above
        if ( $request_filename ~ contenttoberemoved/.+ ) {
            rewrite ^(.*) http://www.website.com/category/articles/ permanent;
    }
    
    # No idea what to do to address #3
    

    这是完整的服务器块:

    server {
        server_name website.com www.website.com;
    
        access_log   /var/log/nginx/access.log;
        error_log    /var/log/nginx/error.log error;
    
        root /usr/share/nginx/www;
        index index.php;
    
        location / {
                try_files $uri $uri/ /index.php?$args;
        }
    
        location ~ \.php$ {
                try_files $uri =404;
                include fastcgi_params;
                #fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_pass 127.0.0.1:9000;
        }
    
        # redirects
    
            if ( $request_filename ~ contenttoberemoved/ ) {
                rewrite ^(.*) http://www.website.com/category/articles/ permanent;
            }
            if ( $request_filename ~ contenttoberemoved/.+ ) {
                rewrite ^(.*) http://www.website.com/category/articles/ permanent;
            }
    
        #many page-level redirects below....
    }
    

    如何使重定向指令足够广泛,以包含示例网址#1&上面的#2,但具体到不包括图像(或者除了我试图定位的帖子以外的任何其他内容)?

    谢谢!

1 个答案:

答案 0 :(得分:1)

对于您的网址,您无需使用if和正则表达式。简单的location就足够了:

location ^~ /contenttoberemoved/ {
    return 301 /category/articles/;
}

这就是全部。