【问题标题】:Convert Apache rewrite to Nginx将 Apache 重写转换为 Nginx
【发布时间】:2020-05-30 14:51:55
【问题描述】:

我有这个 htaccess 代码:

RewriteEngine On
​
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
​
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

我想转换成 nginx 重写规则,我试过了

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

但它不起作用,你知道吗?

【问题讨论】:

    标签: apache .htaccess nginx


    【解决方案1】:

    等效的重写是:

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

    由于nginx URI 包含您的脚本可能不需要的前导/。此外,.php 文件可能在不同的位置块中处理,因此您需要使用last

    没有use of an if block 的其他写法:

    首先,用前导/

    location / {
        try_files $uri $uri/ /index.php?url=$uri;
    }
    

    其次,没有前导/

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

    所有nginx 指令都是documented here

    【讨论】:

      【解决方案2】:

      将您的 .htaccess 转换为 nginx。我刚刚从 Apache 2.4 切换到 Nginx 1.18.0 并且它可以工作,但是所有重写行都应该在 nginx conf 中的服务器块内,否则它会全部转换。

      https://winginx.com/en/htaccess

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-18
        • 2014-11-10
        • 1970-01-01
        • 1970-01-01
        • 2015-02-19
        • 2012-09-02
        • 2012-02-25
        相关资源
        最近更新 更多