TJ Kelly

How to 301 Redirect All URLs Except Certain?

I have a problem. I want to move my blog posts into a /blog/ directory and redirect all current post URLs to their respective new URLs to avoid 404 errors.

With 100+ blog posts, I’d rather redirect the URLs en masse, but here’s the catch: only the blog should be redirected. Pages should remain at their current URLs. So the question becomes: how do I 301 redirect all URLs except pages?

What I’ve tried

I’ve done my share of googling on this. I’ve found a few helpful posts, but nothing has worked for me yet. Here are a few snippets I’ve tried in my .htaccess so far.

RewriteCond %{REQUEST_URI} !^\/(about|contact)\/
RewriteRule ^$ /blog/$1/

…also tried…

RewriteCond %{REQUEST_URI} !^\/about\/
RewriteCond %{REQUEST_URI} !^\/contact\/
RewriteRule ^$ /blog/$1/

Plugins haven’t helped

I’m using a plugin called Redirection. It’s been helpful so far, but it doesn’t help me here. I need to come up with the regex formula on my own to target posts. I posted in Redirection plugin support forum asking that community for help.

Redirection does allow me to import redirects in bulk using htaccess, CSV, or XML. But, again, that assumes I either write them all out individually or figure out on my own how to regex them. That’s the part I need help with.

Could something be conflicting?

I don’t know enough about Apache/mod_rewrite/htaccess/regex to know what each line of my htaccess file does. The above lines are easy enough to understand. But there’s a bunch of other stuff  in that file that I don’t quite get, most of it inserted by WordPress or other plugins.

Here’s a full copy of my htaccess file. Take a look and see if you can spot any errors. Here are (what I think are) the relevant parts:

<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteCond %{REQUEST_URI} !^\/about\/ [NC] #RewriteRule ^$ /blog/ [R,L] RedirectMatch 301 ^/shsjy/(.*)$
RedirectMatch 301 ^/doc/(.*)$
RedirectMatch 301 ^/spartans2/(.*)$
RedirectMatch 301 ^/windows-xp-desktop-%E2%80%8Bwallpapers(.*)$
RedirectMatch 301 ^(.*)/function.(include|session-start|include-once)$$1
RedirectMatch 301 ^(.*)/(Free|FLV|BootSkins)$$1
RedirectMatch 301 ^/umassalumni/campus-info/(.*)$
RedirectMatch 301 ^/umassalumni/campus-news/(.*)$

Any help on how to 301 redirect all URLs except pages would be greatly appreciated!

Leave a Reply

Your email address will not be published. Required fields are marked *