Nginx Rewrite URL replacement for Apache rewrite

守給你的承諾、 提交于 2019-12-12 09:48:50

问题


How do I convert nginx equivalent URL rewrite for the following:

RewriteRule ^read/([0-9]+)/?$ /read/?u=$1 [QSA,L]

回答1:


You can do this in two ways, with a location:

# the ?<u> assigns the capture to $u.  Some older pcres need ?P<u>
location ^/read/(?<u>[0-9]+)/?$ {
  rewrite ^ /read/?u=$u last;
}

or with just a rewrite:

rewrite ^/read/([0-9]+)/?$ /read/?u=$1 last;

nginx will append the query string by default (you can disable the behavior by adding another ? at the end of the rewrite target).




回答2:


Give the following a try:

rewrite ^read/([0-9]+)/$ /read/?u=$1 permanent;

IfIsEvil - hence direct rewrite option.



来源:https://stackoverflow.com/questions/6825111/nginx-rewrite-url-replacement-for-apache-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!