Rewrite WordPress permalink URL using mod_rewrite breaks Archive

The following mod_rewrite rule accomplishes the task of rewriting www.domain.com/2011/11/page to www.domain.com/page but breaks www.domain.com/2011/11/ (i.e. breaks WordPress Archive listings) and redirects it to the root of the site.

The rewrite rule should only rewrite items that have content after ^([0-9]{4})/([0-9]{1,2})/page but not ^([0-9]{4})/([0-9]{1,2})/.

Read More
RewriteRule ^([0-9]{4})/([0-9]{1,2})/(.*)$ /$3 [NC,R=301,L]

Any recommendations?

ANSWER

The initial forward slash was missing at the beginning:

RewriteRule ^/([0-9]{4})/([0-9]{2})/(.*)$ /$3 [NC,R=301,L]

and the WordPress permalink entry needed:

/%postname%/

instead of:

%postname%

although I am not sure how much the latter helped.

Related posts

Leave a Reply

1 comment

  1. I think you need to change the * to a +

    RewriteRule ^([0-9]{4})/([0-9]{1,2})/(.*)$ /$3 [NC,R=301,L]
    

    should be

    RewriteRule ^([0-9]{4})/([0-9]{1,2})/(.+)$ /$3 [NC,R=301,L]
    

    With (.), it could possibly match nothing, thus a request like “/2011/11/” will match but the back reference for (.) will be blank, thus the rewrite goes to “/”. The + indicates that there needs to be at least 1 character satisfying the “.” in the regular expression.