【问题标题】:How to convert .htaccess to a nginx equivalent?如何将 .htaccess 转换为 nginx 等价物?
【发布时间】:2016-07-09 02:16:46
【问题描述】:

如何将.htaccess配置转成nginx?

我的 .htacess:

Options -MultiViews
RewriteEngine On
RewriteBase /mvc/public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

我试过这个:

nginx 配置

location /mvc/public/ {
if (!-e $request_filename){
rewrite ^/mvc/public/(.+)$ /mvc/public/index.php?url=$1 [QSA,L];
}
}

但这没有用! 有人可以帮我吗?

【问题讨论】:

    标签: apache .htaccess mod-rewrite nginx winginx


    【解决方案1】:

    [QSA,L] 不是 nginx 语法 - 有关详细信息,请参阅 this document

    location /mvc/public/ {
        if (!-e $request_filename) {
            rewrite ^/mvc/public/(.+)$ /mvc/public/index.php?url=$1 last;
        }
    }
    

    使用try_files 而不是if 可以实现类似的行为:

    location /mvc/public/ {
        try_files $uri $uri/ @rewrite;
    }
    location @rewrite {
        rewrite ^/mvc/public/(.+)$ /mvc/public/index.php?url=$1 last;
    }
    

    详情请见this document

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-08
      • 2021-06-30
      • 1970-01-01
      • 2016-04-29
      • 2016-05-23
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      相关资源
      最近更新 更多