簡體   English   中英

將 Apache 重寫轉換為 Nginx

[英]Convert Apache rewrite to Nginx

我有這個 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;
}

但它不起作用,知道嗎?

等效的重寫將是:

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

由於nginx URI 包含您的腳本可能不想要的前導/ 此外, .php文件可能在不同的位置塊中處理,因此您需要使用last

使用if塊的替代方法:

首先,使用領先的/

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

其次,沒有前導/

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

此處記錄了所有nginx指令。

將您的 .htaccess 轉換為 nginx。 我剛剛從 Apache 2.4 切換到 Nginx 1.18.0 並且它可以工作,但是所有重寫行都應該在 nginx conf 中的服務器塊內,否則它會全部轉換。

https://winginx.com/en/htaccess

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM