Reputation: 303
I have an issue where I am trying to use nginx to remove some legacy information from a URL however it's a date but it always follows the same format:
http://example.com/blog/xxxx/xx/xx/this-is-a-blog-post/
To..
http://example.com/blog/this-is-a-blog-post/
I wonder if this is possible? I've had a go at trying to write it myself, but I'm having trouble picking out only the middle part. Is this possible with a re-write rule?
Thanks for reading!
Upvotes: 2
Views: 1783
Reputation: 303
After some playing around and testing I managed to achieve this using the following statement:
rewrite "/blog/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)" /blog/$4 permanent;
The first part matches the following expression:
/blog/2013/01/01/
Anything after the last slash will be used to build the new URL, this is used with a $4 as $ can be used to reference each () the first three are year, month, day and the final set matches the title of the blog post which is why I used $4.
Hope this helps people, thanks to Mohammad in the comments for getting me on the correct lines.
Upvotes: 4
Reputation: 42789
You can try this
rewrite /blog/[0-9]{4}(?:/[0-9]{2}){2}(?<new_uri>.*) /blog$new_uri last;
Upvotes: 1