1

I am trying to redirect all visits to a specific sub-folder with a few exceptions.

This is the URL I am trying to redirect:

  • www.example.com/my-sub-folder/

I would like any hits to anything under that URL to be redirected to the following URL:

  • www.example.com/news/category/my-new-sub-folder/

Except, I don't want to redirect these URLs:

  • www.example.com/my-sub-folder/my-special-folder/.*
  • www.example.com/my-sub-folder/wp-admin/.*

I have tried to do the following within my .htaccess file but it does not seem to deal with the exceptions:

RewriteCond %{REQUEST_URI} !^/my-sub-folder/my-special-folder(/.*)?$ [OR]
RewriteCond %{REQUEST_URI} !^/my-sub-folder/wp-admin(/.*)?$ 
RewriteCond %{REQUEST_URI} ^/my-sub-folder(/.*)?$ 
RewriteRule ^(.*)$ /news/category/my-new-sub-folder/ [R=301,L]

What am I doing wrong or is there a better way?

Thanks

1

2 Answers 2

1

It's because you are using OR in the conditions you need to include all the conditions. Try this rule.

RewriteCond %{REQUEST_URI} !^/my-sub-folder/my-special-folder/? [NC]
RewriteCond %{REQUEST_URI} !^/my-sub-folder/wp-admin/? [NC]
RewriteCond %{REQUEST_URI} ^/my-sub-folder/?
RewriteRule ^(.*)$ /news/category/my-new-sub-folder/ [R=301,L]
2
  • I forgot to ask, what does the ? do at the end of the RewriteCond
    – Aaron
    Commented Mar 17, 2015 at 23:52
  • It makes the / optional on the directory so if you have it or not, the condition will still work. Commented Mar 18, 2015 at 13:32
0

Well you tagged it Wordpress, so you could use the Wordpress rewrite API instead of manually adding htaccess rules. Here is the codex: http://codex.wordpress.org/Rewrite_API/add_rewrite_rule Then put the rules in a plugin or your theme's functions.php file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.