问题原文
原帖链接 · Nintendo64 · CC BY-SA 3.0
I'm really confused by the Nginx rewrite rules. I have searched and found a lot of threads for rewrite rules but I don't get the required result...
I have this form of a link:
http://example.com/picture/587
should rewrite to:
http://example.com/picture/587-specialword
the number is dynamic and is changing from URL to URL and only the URLs with the word "picture" should be affected.
So I tried these:
rewrite ^/picture(.*) /picture$1specialword permanent;rewrite ^(.*)picture(.*)$ $1picture$2specialword;
which do not work... and I'm getting this result which leads to an error:
http://example.com/picture/587specialword&specialword&specialword&specialword&specialword&specialword&specialword&specialword&specialword&specialword&specialword&specialword&specialword&specialword&specialword&specialword&specialword&specialword&specialword&specialword
So the word that has been added one time is added multiple times.
Is there any solution for this? What am I doing wrong?
已采纳解答
原帖链接 · MrWhite · CC BY-SA 3.0
rewrite ^/picture(.*) /picture$1specialword permanent;
This would seem to result in a redirect loop since (.*) will match the redirected URL which then gets "specialword" appended again and again, etc.
You can avoid this loop by being more specific in your regex and only checking for what you need, ie. a number. Try something like the following:
rewrite ^/picture/(\d+)$ /picture/$1-specialword permanent;
I assume you want an external redirect (ie. the URL in the address bar should change) and not an internal rewrite?
You will need to clear your browser cache before testing, since permanent (301) redirects are cached by the browser.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
