Nginx重定向来自drupal的旧网址

时间:2018-01-14 09:51:49

标签: nginx

我使用nginx作为webserver

从drupal转移到jekyll

我的旧网址前缀为/?q=permalink

我试图重写它们但它失败了,我不明白为什么。

示例:

http://my.domain.net/?q=my-post-permalink

必须重定向到

http://my.domain.net/my-post-permalink

我的default.conf:

location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    rewrite ^/\?q=(.*)$ $1 break;
    try_files $uri $uri/ $uri.html =404; 
}

日志:

2018/01/14 09:42:17 [notice] 5#5: *1 "^/\?q=(.*)$" does not match "/", client: 127.0.0.1, server: localhost, request: "GET /?q=my-post-permalink HTTP/1.1", host: "my.domain.net"
2018/01/14 09:42:17 [notice] 5#5: *1 "^/\?q=(.*)$" does not match "/index.html", client: 127.0.0.1, server: localhost, request: "GET /?q=my-post-permalink HTTP/1.1", host: "my.domain.net"
127.0.0.1 - - [14/Jan/2018:09:42:17 +0000] "GET /?q=my-post-permalink HTTP/1.1" 200 3520 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0" "-"
2018/01/14 09:42:17 [notice] 5#5: *1 "^/\?q=(.*)$" does not match "/css/foundation.css", client: 127.0.0.1, server: localhost, request: "GET /css/foundation.css HTTP/1.1", host: "my.domain.net", referrer: "http://my.domain.net/?q=my-post-permalink"

1 个答案:

答案 0 :(得分:1)

rewrite指令使用剥离查询字符串的规范化URI。可以使用$arg_变量系列访问查询字符串。有关详情,请参阅this document

您只对重写q URI的/参数感兴趣,因此应使用完全匹配的位置块。有关详情,请参阅this document

例如:

root   /usr/share/nginx/html;
index  index.html index.htm;

location = / {
    if ($arg_q) {
        return 301 /$arg_q;
    }
}

location / {
    try_files ...;
}

有关使用if的信息,请参阅this caution