I think the .htaccess setup for this ought to be fairly straightforward, but I'm having a hard time with it for some reason. I've just completed the migration of a WordPress single site installation to a MultiSite setup, and everything went swimmingly. Now, though, I need to redirect all the original blog posts to /blog/<post> (because WordPress enforces that on MultiSite).

I also recently migrated everything to the naked domain. This in and of itself wasn't a problem – until I added in the /blog/ component.

Here is what I have for the naked domain redirect (which, as noted, works fine):

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

I've tried a number of things for the /blog/ redirect, but it's continuing to throw me. It seems like it should be fairly easy with RedirectMatch:

RedirectMatch 301 ^/(.*/?)$ http://<my-site>/blog/$1

The problem, of course, is that this expression then captures /blog/ and promptly heads off to /blog/blog/blog/...

I run into the same problem using another RewriteCond/RewriteRule pair, and when I tried to work around it like this:

RewriteCond %{HTTP_HOST} ^(?:(www\.))?(.*)$ [NC]
RewriteRule ^(?<!(blog/))(.*)$ http://%1/blog/$1 [R=301,L]

The result was a redirect loop (I'm not seeing what's causing it, but my head is obviously not processing the regexes correctly this morning).

It seems to me like RedirectMatch is the way to go anyway; I just need to make sure not to capture /blog/ itself.

Edit: I totally forgot about the ! part of the rule; should I simply be starting that RewriteMatch with !^blog?


You could just do this:

RewriteCond %{REQUEST_URI} !^/blog
RewriteRule ^(/?)(.*) /blog/$2 [L,R=301]

then anything that doesnt have a /blog in it will be rewritten to have the /blog in it.

(referenced from https://stackoverflow.com/questions/4844069/htaccess-rewritebase)

| improve this answer | |
  • That looks like it would indeed probably work. I'll have to try it out sometime. (On the site I originally needed this for, I got around it another way, by getting rid of the /blog component entirely—something not easy but ultimately doable in the WordPress back end). – Chris Krycho Jun 8 '13 at 21:57
  • thanks, I didnt actually test it, but it should work :D – Jonathan Jun 10 '13 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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