Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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 http://stackoverflow.com/questions/4844069/htaccess-rewritebase)

share|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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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