如何使用NGINX将mysite.com/page/重写为mysite.com/page/index.html?

时间:2012-01-25 21:47:23

标签: url-rewriting nginx

如何使用nginx 301将mysite.com/page/重写为mysite.com/page/index.html?

在Apache我有:

RewriteRule page/ http://mysite.com/page/index.html [R=301,L]

感谢您的帮助,

hydn

2 个答案:

答案 0 :(得分:1)

尝试以下设置:

location / {
    rewrite /page/ http://mysite.com/page/index.html permanent;
    ...
}

答案 1 :(得分:1)

我从你对Sergiei的评论中看到'/ page /'目录和'/page/index.html'实际上并不存在,并在其他地方重写。所以Nginx没有找到404就不足为奇了。

如果访问者请求'/page/index.html',应该准确提供什么?是的,那会被改写成什么?

如果是index.php?q = / page / index.html,那么你的配置应该是:

server {
    # index directive should be in server or http block
    # So no need to repeat in every location block
    # unless specifically overriding it
    index index.php index.html;
    location / {
        rewrite ^/page(/?)$ /page/index.html break;
        try_files $uri $uri/ /index.php?q=$uri;
    }
}

您也可以使用

server {
    index index.php index.html;
    location / {
        try_files $uri $uri/ /index.php?q=$request_uri;
    }
}

但可能存在一些问题。一切都取决于您的申请细节。