简体   繁体   中英

nginx rewrite to remove date path of legacy URLs

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!

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.

你可以试试这个

rewrite /blog/[0-9]{4}(?:/[0-9]{2}){2}(?<new_uri>.*) /blog$new_uri last;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM