I have this rule:
RewriteCond %{REQUEST_URI} ^/(manhattan|queens|westchester|new-jersey|bronx|brooklyn)-apartments/.*$
RewriteCond %{REQUEST_URI} !^/guide/(.*)$
RewriteRule ^(.*)$ /home/neezer/public-html/domain.com/guide/$1 [L]
Which works great on it's own. Essentially, I have a bunch of directories that have a bunch of files in them that I want to keep in the "/guide" folder, but I want them to appear at the web root for SEO reasons.
This rule works, but unfortunately the original URL's still work too (with "/guide"). I want to 301 Redirect the ones with "/guide" in the URL to those without, without actually moving the files on the server.
I tried adding this rule:
RewriteCond %{REQUEST_URI} ^/guide/(manhattan|queens|westchester|new-jersey|bronx|brooklyn)-apartments/.*$
RewriteRule ^guide/(.*)$ http://www.domain.com/$1 [R=301,L]
... but that breaks my first rule completely.
Any thoughts about what I might be doing wrong? Please let me know if you need to know anything else from me to help me with this issue.
EDIT: Per Matthews suggestion, I've changed my rules to this (reflecting the behavior I want):
RewriteRule ^guide/(manhattan|queens|westchester|new-jersey|bronx|brooklyn)-apartments/(.*)$ http://www.domain.com/$1-apartments/$2 [R=301,L]
RewriteRule ^(manhattan|queens|westchester|new-jersey|bronx|brooklyn)-apartments/(.*)$ /home/neezer/public-html/domain.com/guide/$1-apartments/$2 [L]
However, the rules are still breaking each other, just in a different way this time. Trying to access a page governed by the second rule (and what the first rule correctly redirects to) give me this error in Safari:
Too many redirects occurred trying to open “http://www.domain.com/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php”. This might occur if you open a page that is redirected to open another page which then is redirected to open the original page.
Any suggestions?
FURTHER EDIT:
It seems that these rules are looping onto themselves. Here's an excerpt from the rewrite logs:
init rewrite engine with requested uri /manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php
pass through /manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php
[perdir /home/neezer/public-html/domain.com/] rewrite 'manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php' -> '/home/neezer/public-html/domain.com/guide/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php'
[perdir /home/neezer/public-html/domain.com/] strip document_root prefix: /home/neezer/public-html/domain.com/guide/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php -> /guide/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php
[perdir /home/neezer/public-html/domain.com/] internal redirect with /guide/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php [INTERNAL REDIRECT]
init rewrite engine with requested uri /guide/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php
pass through /guide/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php
[perdir /home/neezer/public-html/domain.com/] rewrite 'guide/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php' -> 'http://www.domain.com/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php'
[perdir /home/neezer/public-html/domain.com/] explicitly forcing redirect with http://www.domain.com/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php
[perdir /home/neezer/public-html/domain.com/] escaping http://www.domain.com/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php for redirect
[perdir /home/neezer/public-html/domain.com/] redirect to http://www.domain.com/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php [REDIRECT/301]
I believe the problem lies in the section the middle section (which I separated by line breaks). It seems after Apache rewrites to the local path (using /home/neezer/...), it issues another request with the document root stripped, which of course includes /guide, which then filters in the first rule, which eventually filters in the second rule, and back and forth onto infinity.
How do I tell it to simply STOP after the first rule? Adding [L] seems to have no effect.