views:

66

answers:

2

I have a blog installed in www.foo.com/wp/ and would like all requests that go to any page that doesn't start with /wp/ to be redirected to www.foo.com/wp/ (without preservation of the URL).

E.g.:

www.foo.com/bar/baz/hello.gif     > www.foo.com/wp/

Also, at the same time, I need the URL to have www. added if it doesn't (which might be where my rules are getting confused)

At the moment, I have:

RewriteCond %{HTTP_HOST}      !^www.foo.com [NC]
RewriteCond %{HTTP_HOST}      !^$
RewriteRule ^/(.*)            http://www.foo.com/$1 [R=permanent]

RewriteRule ^(?!wp)/(.*)$               http://%{HTTP_HOST}/wp/($2) [R=permanent]

But I think this is currently completely broken.

Help much appreciated.

+1  A: 
RewriteCond %{HTTP_HOST}      !^www.foo.com [NC]
RewriteCond %{HTTP_HOST}      !^$
RewriteRule (.*)              http://www.foo.com/$1 [R=permanent,L]

RewriteRule ^(?!wp/)(.*)      http://%{HTTP_HOST}/wp/$1 [R=permanent]
  1. The path portion of the URL being matched never has a leading slash, and you don't need to anchor a .*.
  2. L modified prevents rewriting from continuing after you arrive at your first redirect.
  3. Negative-lookbehind (?!) is not a capturing construct, and you substitute in captured patterns by writing $1, not ($1).

Edit: Apparently for OP the last rule doesn't work, failing to exclude the cases that begin with /wp/, which makes no sense to me but whatever. Here's an attempt at a workaround:

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

RewriteCond %{REQUEST_URI}    !^/wp/
RewriteRule (.*)              http://%{HTTP_HOST}/wp/$1 [R=permanent]
chaos
For the /wp/ part, this creates a redirect loop, adding an endless number of /wp to the URL.
Thanks chaos. This ended up giving me the behaviour I was after:RewriteCond %{REQUEST_URI} !^/wpRewriteRule (.*) http://%{HTTP_HOST}/wp$1 [R=permanent]
A: 

Try these rules:

RewriteCond %{HTTP_HOST} !^www.exmaple.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L]

RewriteCond %{THE_REQUEST} ^GET\ /wp/
RewriteRule ^wp/(.*) /$1 [R=301,L]

RewriteRule !^wp/ wp%{REQUEST_URI} [L]

The first rule is for the host name. The second is to remove the /wp path prefix externally. And the third is to add the prefix again for internal redirection.

Gumbo