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?


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.


I'm not sure why you're using the RewriteConds. It seems to me that you could put everything directly in the RewriteRules. It looks like your problem stems from the fact that you're redirecting everything that matches the Request_URI. If you do the matching in the RewriteRule itself the rewritten rule will no longer match the next rule.

You might want to try something like:

RewriteRule ^guide/(manhattan|queens|westchester|new-jersey|bronx|brooklyn)-apartments/(.*)$ http://www.domain.com/$2 [R=301]
RewriteRule ^(manhattan|queens|westchester|new-jersey|bronx|brooklyn)-apartments/(.*)$ /home/neezer/public-html/domain.com/guide/$2 

You'll probably need to tweak these because I don't understand all of what you're trying to accomplish. For example the RewriteCond below seemed redundent so I left it out. If you really are trying to rewrite everything under the guide directory you'll have to incorporate it back in.

RewriteCond %{REQUEST_URI} !^/guide/(.*)$

After doing some quick testing on my server I was able get these rules work but not in a per-directory context (ie. not in a .htaccess or inside a <Directory></Directory>).

I didn't test this but you might be able to work around this by setting an evironrnment variable with the second rule and then testing for it on the first.

RewriteCond %{ENV:REDIRECT} !^NO$
RewriteRule ^guide/(manhattan|queens|westchester|new-jersey|bronx|brooklyn)-apartments/(.*)$ http://www.domain.com/$2 [R=301]
RewriteRule ^(manhattan|queens|westchester|new-jersey|bronx|brooklyn)-apartments/(.*)$  [E=REDIRECT:NO]
  • Thanks for responding. I included my tweaked rules above to reflect what I want, but now I'm getting a "Too many redirects" error. See my edit above for more of an explanation. Do you have another suggestion? – neezer Jun 18 '10 at 13:46

I couldn't find anyway around that infinite loop, so I moved the files to a new folder called "neighborhoods" and used these rules:

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/neighborhoods/$1-apartments/$2 [L]

Less than ideal, because now I have two folders (I have other files I'm serving out of guide), but it does address my SEO concerns.

  • Wouldn't redirecting the other direction give you the same affect SEO-wise? Also see my edit below. – matthew Jun 18 '10 at 21:02

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.