title: Using Apache mod_rewrite for conditional redirection if URL exists
body: <p>I'm migrating a large site (with significant changes to each page), and want a test group of users to be automatically redirected from the current site to the new site as each page is completed.</p>
  
  <p>What was</p>
  
  <pre><code>www.mysite.com/admin/somefile.php
  </code></pre>
  
  <p>will become</p>
  
  <pre><code>admin.mysite.com/somefile.php
  </code></pre>
  
  <p>As each page is migrated to the new site, I want users from a specific IP address to be automatically redirected to it. This looks like a case for the '-U' (existing URL) flag of RewriteCond. The mod_rewrite statements (for www.mysite.com) would be something along the lines of</p>
  
  <pre><code>  RewriteEngine On
    RewriteCond %{REMOTE_ADDR} 123\.123\.123\.123           # for test users only
    RewriteCond http://admin.mysite.com/$1 -U               # if new page exists
    RewriteRule /admin/(.*) http://admin.mysite.com/$1 [R=302,L]  # then redirect
  </code></pre>
  
  <p>However, this doesn't seem to work - perhaps because the $1 parameter is being referenced before being defined?</p>
  
  <p>Can anyone advise how this can be achieved?</p>
  
  <p>Thanks, Chris</p>