301重定向不会删除NGINX中的原始路径

时间:2014-04-10 08:33:39

标签: .htaccess mod-rewrite redirect nginx

我尝试使用rewrite |将特定网址重定向到新网址301 redirect由NGINX提供。

if ($request_uri = "/playlist/show/531a5aaefd3401705c000a32") {
   rewrite ^.*$ playlist/all/ redirect;
}

如果我的请求URI为http://servername.com/playlist/show/531a5aaefd3401705c000a32,则应重定向到http://servername.com/playlist/all/

但是现在发生的事情是旧网址的原始路径仍然存在:

http://servername.com/playlist/show/playlist/all/

/playlist/show/斜杠/路径仍然存在,因此它会重定向到错误的URI。

我在处理NGINX时非常新。请多多包涵。谢谢!

编辑:

这是我更明确的说法:

server {
  listen 80;
  server_name site.servername.com;
  server_tokens off;
  charset utf-8;
  root /home/site/stm/public;

  location / {
    index index.php;
    # auth_basic "Restricted";
    # auth_basic_user_file /home/site/stm/.htpasswd;
  }

  location = /favicon.ico {
    access_log off;
    log_not_found off;
    empty_gif;
    expires 30m;
  }

  if ($request_uri = "/playlist/show/531a5aaefd3401705c000a32") {
    rewrite ^.*$ playlist/all/ redirect;
  }

  if ($request_uri = "/favicon.ico") {
    rewrite ^.*$ /favicon.ico last;
  }

  if ($request_uri !~* ^/(?:static|favicon.ico)) {
    rewrite ^.*$ /index.php last;
  }


  location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}

1 个答案:

答案 0 :(得分:0)

你做错了:http://wiki.nginx.org/Pitfalls

你永远不应该使用if ($uriif ($request_uri,因为它只是制作location的一种非常糟糕的方法。

location =/playlist/show/531a5aaefd3401705c000a32 {
    return 302 /playlist/all/;
}

简单,快速,无正则表达式。

参考研究: