大网数据广告活动
网站运营与故障问答

(nginx rewrite) add word on dynamic URL ending | word is repeated multiple in URL

· 52CE 我爱测

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: htt…

问题原文

原帖链接 · 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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2017-03-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。