Reputation: 2736
We are migrating our site from Wordpress to another blog and we are looking for a way to redirect all users using the "old" URL structure of /blog/date/title to the new which is /blog/title. There are over 2000 posts so I would really rather not have to do these manually!
For example, the current URL might be:
http://www.example.com/blog/2013/06/this-is-the-title/
and we would like that redirected to:
http://www.example.com/blog/this-is-the-title/
The structure will always be
/blog/yyyy/mm/title
Is it possible to ignore part of a url using htaccess or is this going to need to be done using regex looking for the date pattern?
Upvotes: 0
Views: 380
Reputation: 784898
You can use this redirect rule as your very first rule in site root .htaccess on old site:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(blog)/\d+/\d+/(.+)$ [NC]
RewriteRule ^ /%1/%2 [L,R=301,NE]
Upvotes: 1
Reputation: 751
Assumption, there can't be the same slug for 2 different posts, which I think is a given in wordpress.
Off the top of my head, there are 2 ways you can do this:
1: Use a simple htaccess redirect for this. Now, I am not an expert in htaccess, but I think its something like this:
RewriteRule ^blog/(\d{4})/(\d{2})/(.+)$ /blog/$3 [L]
2: This one, is a little tricky, but can get flexible results according to your need. Since the old URLs don't exist, they result in the 404 page. Now, make that 404 page such that in it you search for a post, If you find a match then redirect the user to that page without responding with the 404 status. If their is no match, you can show them most popular/ most similar results etc etc. I don't understand why people do this more often. Showing suggestions based on your query should be common on the 404 page.
Let me know if you need more help.
Upvotes: 2