2

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?

1
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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.