nginx 301永久重定向网址到页面/文件夹

时间:2015-07-07 11:03:29

标签: redirect nginx http-status-code-301

我需要重定向几个网址。 301永久

http://example.com/downloads/example.exehttp://example.com/emaple-page/

我需要为3个URL执行此操作,我将在.conf文件

上执行此操作
 server {
    listen 80;
    server_name http://example.com/downloads/example.exe;

    rewrite ^/(.*) http://example.com/emaple-page/$1 permanent;
}
    server {
    listen 80;
    server_name http://example.com/downloads/example2.exe;

    rewrite ^/(.*) http://example.com/emaple-page/$1 permanent;
}
    server {
    listen 80;
    server_name http://example.com/downloads/example3.exe;

    rewrite ^/(.*) http://example.com/emaple-page/$1 permanent;
}

我想知道这是对的吗?最好的方法是什么?

1 个答案:

答案 0 :(得分:0)

每个域只能包含1个server部分。您可以将原始网址直接放在rewrite子句中。

server {
  listen 80;
  server_name example.com;
  index index.html index.php
  rewrite ^/downloads/example\.exe  http://example.com/emaple-page/  permanent;
  rewrite ^/downloads/example2\.exe http://example.com/emaple-page2/ permanent;
  rewrite ^/downloads/example3\.exe http://example.com/emaple-page3/ permanent;
}