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

Url rewriting 如何使用NGINX将mysite.com/page/重写为mysite.com/page/index.html?,url-rewriting,nginx,Url Rewriting,Nginx,如何使用nginx将mysite.com/page/重写为mysite.com/page/index.html 在Apache中,我有: RewriteRule page/ http://mysite.com/page/index.html [R=301,L] 谢谢你的帮助 hydn尝试以下设置: location / { rewrite /page/ http://mysite.com/page/index.html permanent; ... } 我从您对Sergiei

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

在Apache中,我有:

RewriteRule page/ http://mysite.com/page/index.html [R=301,L]
谢谢你的帮助

hydn

尝试以下设置:

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

我从您对Sergiei的评论中看到,“/page/”目录和“/page/index.html”实际上并不存在,并且在其他地方被重写。所以Nginx给出一个404 not found就不足为奇了

如果访问者请求“/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;
    }
}

但这可能有一些问题。这一切都取决于您的应用程序的详细信息。

谢谢。我会努力的。包括…?目录页是否存在?试着把“重写”放在第一行,然后放上Try_文件。如果dir和index.html可能不存在其友好URL,请提供查询示例。请参见下面的评论,这也是后端PHP脚本的一个问题。一旦修好了,一切都很好。