Nginx rewrite to redirect URL within domain
When I moved over to Ghost blogging platform, I chose Dated Permalinks, an option that allows for including dates in blog post URL. Realizing that the blog post URL is unique even without the Dated Permalinks option, I opted out of it. But by then I had already posted a few of my blog links on Dzone. This broke my links on Dzone. To fix that I used Nginx's
rewrite module
Nginx's rewrite module allows you to use regular expression like so:
rewrite ^/2014/11/03(/email-no.*/)$ $1 break;
Above, I am stating that:
- For a URL starting with
/2014/11/03/
AND - Followed by
email-no
then capture the part starting withemail-no
till the end of the URL.
Then, replace the URL with captured part of the URL (i.e. $1).
So then:
/2014/11/03/email-notifications-via-supervisor-on-abrupt-termination-of-process/
gets translated to:
email-notifications-via-supervisor-on-abrupt-termination-of-process/
Since I have the rewrite rule inside the location
block with path of /
, the rewrite module gets served URL part after the base URL.
For completeness sake, the relevant part of the location
block is provided below:
location / {
rewrite ^/2014/11/03(/email-no.*/)$ $1 break;
rewrite ^/2014/11/14(/throw-dy.*/)$ $1 break;
}
Done.