I've been looking around for the answer to this but haven't been able to solve it myself. What I'm trying to do is redirect a group of around 1500+ URLs from one format to another, where the only thing that's consistent between the two is a string of numbers at the end.
If it's relevant, the old URLs are from a site in PHP and the new URLs are going to be for a WordPress site, so obviously I'd need to make use of regex and htaccess.
The format of the current URL is as follows:
[domain]/[one or more categories separated by a forward slash]/[post name]/[id]
Due to a restriction in Wordpress' permalink system, the new URL structure will be:
[domain]/[one or more categories separated by a forward slash]/[post name]-[id]
In theory is that I can match on the ID and make the middle part completely arbitrary but this doesn't seem to be working. It may be that WordPress' permalinks aren't as flexible as I'm assuming but the below are the two rules I've tried so far:
RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/(\d*)(/)$ http://localhost/ats-new/([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)(-)$3/ [R=301,NC,L]
RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/(\d*)(/)$ http://localhost/ats-new/([A-Za-z0-9\-]+)/$2-$3(/) [R=301,NE,NC,L]
I'm trying to match on the number at the end as this will be consistent between the two URLs and is guaranteed to always be unique to each post. It's also worth noting that the number of categories won't be the same even if the ID is because of the differences in the way categories are stored between the two systems.
My problem is that I can't guarantee that any part of the URL except for the ID will be an identical match between the two due to differences in the way that spaces are escaped between the original code and WordPress' string escaping. The rules I've linked above don't work consistently either, but when they did they would send me to a URL that looked like:
I'm probably missing something fairly simple here but I've been going around in circles so I thought I'd ask here to see if anyone can help shed light on this. I've already looked around but wasn't able to find anything that seemed to be along the same lines as what I'm looking for. Existing topics like the ones I've linked below didn't really provide me with an answer that I could use to solve this myself.
Thanks in advance.