Url .htaccess重写为nginx配置

Url .htaccess重写为nginx配置,url,redirect,nginx,rewrite,Url,Redirect,Nginx,Rewrite,需要将.htaccess重写规则转换为nginx config的帮助。这些规则来自metro shrink url shortner脚本 RewriteEngine On #RewriteBase / RewriteRule ^developer.html$ developer.php [QSA,L] RewriteRule ^multishrink.html$ multishrink.php [QSA,L] RewriteRule ^stats.ht

需要将.htaccess重写规则转换为nginx config的帮助。这些规则来自metro shrink url shortner脚本

RewriteEngine On 
#RewriteBase /

RewriteRule ^developer.html$            developer.php [QSA,L]
RewriteRule ^multishrink.html$          multishrink.php [QSA,L]
RewriteRule ^stats.html$                public-stats.php [QSA,L]
RewriteRule ^feed.rss                   feed.php [QSA,L]
RewriteRule ^([^/]+)\.qrcode$           qrcode.php?id=$1 [QSA,L]
RewriteRule ^api.php$                   API/simple.php [QSA,L]
RewriteRule ^API/write/(get|post)$      API/write.php?method=$1 [QSA,L]
RewriteRule ^API/read/(get|post)$       API/read.php?method=$1 [QSA,L]
RewriteRule ^([^/]+)/stats$             stats.php?id=$1 [QSA,L]
RewriteRule ^([^/]+)/unlock$            unlock.php?id=$1 [QSA,L]

# If path is not a directory or file then apply RewriteRule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d        
RewriteRule ^([0-9a-zA-Z-]{1,60})$      go.php?id=$1 [QSA,L]
尝试:


下面应该可以做到这一点:

location = /developer.html { rewrite ^ developer.php last; }
location = /multishrink.html { rewrite ^ multishrink.php last; }
location = /stats.html { rewrite ^ stats.php last; }

location ~ ^feed.rss { rewrite ^ feed.php last; }
location ~ ^([^/]+)\.qrcode$ { rewrite ^([^/]+)\.qrcode$ qrcode.php?id=$1 last; }

location = /api.php { rewrite ^ API/simple.php; }
location ~ ^API/(write|read)/(get|post)$ { 
  rewrite ^API/(write|read)/(get|post)$ API/$1.php?method=$2 last;     
} 

location ~ ^([^/]+)/stats$ { rewrite ^([^/]+)/stats$ stats.php?id=$id last;}

location ~ ^([^/]+)/unlock$ {
  set $id $1;
  rewrite ^ unlock.php?id=$id last;  
}

location ~ "^([0-9a-zA-Z-]{1,60})$" {
  try_files $uri $uri/ go.php?id=$1;
}
注意:您可以通过先保存位置正则表达式中的捕获来重新使用它们(如在解锁位置中),也可以重复正则表达式(需要保存的原因是rewrite指令重置捕获变量)。用你喜欢的样式

注意:nginx默认情况下会附加查询参数,因此apache的qsa标志基本上是默认的,来自:


谢谢,在我将所有的“break”改为“last”后,它工作了。谢谢你的回复,但是我得到了两个错误1)无效参数“=404”2)在“set”方向中的参数数量无效另一个错误是“^([0-9a-zA-Z-]”中的[emerg]pcre_compile()失败丢失)pcre_编译是因为正则表达式包含大括号,大括号也用于描绘块,因此必须引用正则表达式。在我的回答中修复了无效参数=404,正如set指令一样(导致=404的原因是,在决定使用“重写”之前,我保留了一部分try_文件,该文件更清晰,我忘记添加第二个参数,告诉它wat将$id设置为),两者都已修复
location = /developer.html { rewrite ^ developer.php last; }
location = /multishrink.html { rewrite ^ multishrink.php last; }
location = /stats.html { rewrite ^ stats.php last; }

location ~ ^feed.rss { rewrite ^ feed.php last; }
location ~ ^([^/]+)\.qrcode$ { rewrite ^([^/]+)\.qrcode$ qrcode.php?id=$1 last; }

location = /api.php { rewrite ^ API/simple.php; }
location ~ ^API/(write|read)/(get|post)$ { 
  rewrite ^API/(write|read)/(get|post)$ API/$1.php?method=$2 last;     
} 

location ~ ^([^/]+)/stats$ { rewrite ^([^/]+)/stats$ stats.php?id=$id last;}

location ~ ^([^/]+)/unlock$ {
  set $id $1;
  rewrite ^ unlock.php?id=$id last;  
}

location ~ "^([0-9a-zA-Z-]{1,60})$" {
  try_files $uri $uri/ go.php?id=$1;
}
 If a replacement string includes the new request arguments, the previous request 
 arguments are appended after them. If this is undesired, putting a question mark
 at the end of a replacement string avoids having them appended