Mobile app version of vmapp.org
Login or Join
Candy875

: (nginx rewrite) add word on dynamic URL ending | word is repeated multiple in URL I'm really confused by the Nginx rewrite rules. I have searched and found a lot of threads for rewrite rules

@Candy875

Posted in: #ModRewrite #Nginx #UrlRewriting

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:
example.com/picture/587

should rewrite to:
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(.*) /picturespecialword permanent;
rewrite ^(.*)picture(.*)$ picturespecialword;


which do not work... and I'm getting this result which leads to an error:


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?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Candy875

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ogunnowo487

rewrite ^/picture(.*) /picturespecialword 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/-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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme