NGINX - url rewrite regular expression

谁说胖子不能爱 提交于 2019-12-11 03:44:07

问题


I have the url such as example.com/page.php?username=test. I want to rewrite this url into something like: example.com/test only if test follows the following regual expression: /^[0-9a-zA-Z_-]{1,35}+$/, else 404 page.


回答1:


Try this:

# output: example.com/test
rewrite ^/([A-Za-z0-9_]+)$ /page.php?username=$1;

UPDATE:

{1,35} This expression allow from 1 to 35 character

{20} That have to exactly 20 character

The + say minimum 1 character

The correct full rewrite rule:

# output: example.com/test
rewrite "^/([A-Za-z0-9_]{1,35})$" /page.php?username=$1;


来源:https://stackoverflow.com/questions/20887401/nginx-url-rewrite-regular-expression

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