Quick Regex To Redirect from MM-DD-YYYY to YYYY-MM-dd
I am not sure why I chose to format the urls the way I did from the beginning but I have been wanting to make it into the more proper yyyy-mm-dd format for a while now.
I finally fixed it today as I was fixing a few other things left over from when my weather station was not functioning. I wanted to switch from the URLs in the fashion below From
https://example.com/wx/history/2024/07-21-2024/
to
https://example.com/weather/history/2024/2024-07-21/
With a little help from AI I got it working by using this. I was a bit rusty with regular expressions since I don’t use it often enough that it stays fresh in my memory plus I am not getting any younger.
I just dropped his into the .htaccess file and all is well.
RewriteRule ^wx/history/(\d{4})/(\d{2})-(\d{2})-(\d{4})(.*)$ /weather/history/$1/$4-$2-$3$5 [R=301,L]
While there I took the time to change the entire wx directory to weather and set the redirect to handle that as well.