Reputation: 89
I just changed my wordpress permalinks from domain.com/%year%/%month%/%post-slug%
over to domain.com/%post-slug%
I used the following mod_rewrite
rule to redirect all my older URLs to the new ones.
RedirectMatch 301 ^/[0-9]{4}/[0-9]{2}/([a-z0-9\-/]+) http://www.site.com/$1
The problem being this redirected all my date based archives
domain.com/%year%/%month%/%date%
to
domain.com/%date%
as well which gave out a 404
error. I modified the above code to include a character count (a minimum of 3
and a maximum of 300
) like this in order to tackle the date problem
RedirectMatch 301 ^/[0-9]{4}/[0-9]{2}/([a-z0-9\-/]{3,300}+) http://www.site.com/$1
But am now getting a 500 Internal Server error
.
Any help on this would be appreciated.
Upvotes: 2
Views: 157
Reputation: 12321
I'm not sure what %post-slug% looks like, so I don't know if there is a better way to distinguish between that and a %date%, but I'm inferring that %date% is a 2-digit number, so you can use a negative lookahead assertion to exclude it. Also, you might want to use \d instead of [0-9] (they're interchangeable, but the first one is shorter).
RedirectMatch 301 ^/\d{4}/\d{2}/(?!\d{2}$)([a-z0-9\-/]+) http://www.site.com/$1
That's more reliable than checking the number of characters, unless you're certain that %post-slug% can never be less than three characters. Of course, if %post-slug% can ever be a 2-digit number, the rule I suggested will skip it. But in that case, there's no way for a regex to tell the difference.
Also, I'm assuming, based on what you wrote, that nothing can come after %date%. If there can be another part after that, change the rule to this:
RedirectMatch 301 ^/\d{4}/\d{2}/(?!\d{2}($|/))([a-z0-9\-/]+) http://www.site.com/$2
(Note the $2 at the end instead of $1)
Upvotes: 1
Reputation: 51711
Change the rule to
RedirectMatch 301 ^/[0-9]{4}/[0-9]{2}/([a-z0-9/-]{3,})$ http://www.site.com/$1
Upvotes: 1