regex - How to redirect attachment URL after permalink change -
say example blog permalink structure (year/month/title)
http://blog.example.com/2012/04/my-title
and posts permalinks are:
http://blog.example.com/my-title
to make sure google search results don't break want add .htaccess redirect. using yoast permalink redirector generator on website suggests adding:
redirectmatch 301 ^/([0-9]{4})/([0-9]{2})/([^/]+)/$ http://blog.example.com/$3
at first glance works great. wordpress has attachment urls such as:
http://blog.example.com/2012/04/my-title/my-attachment-url/
that link should redirected to:
http://blog.example.com/my-title/my-attachment-url/
but it's not when using yoast redirect. thoughts on how update .htaccess regex catch attachment urls well? i'll keep googling in mean time. can post on wp stack overflow tends better answers server regex htaccess questions.
i think regex right. need escape slashes backslash:
^\/([0-9]{4})\/([0-9]{2})\/([^/]+)\/$
actually capture attachment urls surround last capturing group says "match that's not slash greedily", plus slash, new group match 1 more more times greedily:
^\/([0-9]{4})\/([0-9]{2})\/(([^/]+)\/)+$
also, can add ?: groups make them non-matching.
Comments
Post a Comment