Search code examples
regexwordpress.htaccessmod-rewritemod-alias

Exlude a directory in .htaccess with RedirectMatch for Wordpress


I change my Wordpress permalink structure from %year%/%months%/ to %postname%

So i create this mod alias rules in .htaccess but i have a problem:

RedirectMatch 301 /\d{4}/\d{2}/([^/]+)(/?)(.*)$ http://domainname.com/$1

The rules also redirect the images under the wp-content directory, so domainname.com/wp-content/uploads/2013/11/name.jpg become domainname.com/name.jpg

I can't use RewriteCond because this RedirecMatch is mod_alias, i try to use !^/(wp-content.*)$ but doesn't work.

How can i solve? It's better to use RedirectMach or RewriteRule (i haven't changed the server)?

Can you post a better regex?

Thank you!


Solution

  • You should probably stick with using mod_rewrite instead of mod_alias because it'll interfere with wordpress' mod_rewrite rules. Both mod_rewrite and mod_alias affect the same request URI at different points in the URL-file processing pipeline, so you could end up getting redirected and rewritten at the same time.

    RewriteCond %{REQUEST_URI} !\.(jpeg|gif|png)$ [NC]
    RewriteRule ^\d{4}/\d{2}/([^/]+?)(/?)(.*)$ http://domainname.com/$1 [L,R=301]