title: Rewrite part of URL with a specific query string to different domain
body: <p>I want to rewrite a set of url's to a different domain only when a specific query string is set. Only the main domain url needs to be rewritten, the rest of the url path needs to stay the same and the query string needs to be removed too.</p>
  
  <p>Examples:</p>
  
  <pre><code>http://www.domain-a.com/post-type-a/post-title/?template=custom
  </code></pre>
  
  <p>should redirect to:</p>
  
  <pre><code>http://www.domain-b.com/post-type-a/post-title/
  </code></pre>
  
  <p>and</p>
  
  <pre><code>http://www.domain-a.com/post-type-b/post-title/?template=custom
  </code></pre>
  
  <p>should redirect to:</p>
  
  <pre><code>http://www.domain-b.com/post-type-b/post-title/
  </code></pre>
  
  <p>and</p>
  
  <pre><code>http://www.domain-a.com/post-type-c/post-title/?template=custom
  </code></pre>
  
  <p>should redirect to:</p>
  
  <pre><code>http://www.domain-b.com/post-type-c/post-title/
  </code></pre>
  
  <p>etc.</p>
  
  <p>The query string is set to load different single-post templates, this way I can make a micro site within my excisting wordpress website. However I want to use my other domainname for this microsite, therefore my question.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>If I put these lines in my .htaccess in the root of domain-b:</p>
  
  <pre><code>RewriteCond %{HTTP_HOST} domain-a\.com$ [NC]
  Rewritecond %{QUERY_STRING} ^template=custom$ [NC]
  RewriteRule ^ http://www.domain-b.com%{REQUEST_URI}? [R=301,L]
  </code></pre>
  
  <p>the redirection actually works, however I run into 2 problems:</p>
  
  <p>1) The correct template isn't loaded because the rewrite rule has allready taken off the query string before the function can detect it.<br>
  2) <a href="http://www.domain-b.com" rel="nofollow">http://www.domain-b.com</a> changes back to www.domain-a.com because of the other rewrite rules.</p>
  
  <p>So I think I have to find the solution for problem 1 within my wordpress function instead of .htaccess. Let me explain my situation better:</p>
  
  <p>My main domain with webspace is registered on <strong>domain-b</strong>. My wordpress website is installed in the folder <strong>blog</strong>. I'm using <strong>domain-a</strong> as wordpress url, so <strong>domain-b.com/blog/</strong> redirects to <strong>domain-a</strong>.
  That's working fine, but I want to make an exception on these wordpress rewriting rules if the query string is set to <strong>template=custom</strong> like above. In my .htaccess file in the root of the folder <strong>blog</strong> I have the following rules (generated by wordpress):</p>
  
  <pre><code>&lt;IfModule mod_rewrite.c&gt;
  RewriteEngine On
  RewriteBase /blog/
  RewriteRule ^index\.php$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /blog/index.php [L]
  &lt;/IfModule&gt;
  </code></pre>
  
  <p>This is the function I use to load the different single-post template files:</p>
  
  <pre><code>// Add Query var
  add_filter('query_vars', 'template_query_vars');
  function template_query_vars( $query_vars ){
      $query_vars[] = 'template';
      return $query_vars;
  }
  
  // Switch template according to query parameter
  add_filter( 'template_redirect', 'sjc_template' );
  function sjc_template(){
      global $wp_query;
      if( $wp_query-&gt;get( 'template' ) ):
          global $post;
          $posttype = get_post_type($post-&gt;ID);
          include( get_template_directory() . '/single-'. $posttype .'-custom.php' );
          exit();
      endif;
  }
  </code></pre>
  
  <p>So I tried putting the rewrite rule in there, but that's not working (yet):</p>
  
  <pre><code>// Set up the rewrite
  add_action( 'init', 'template_setup_rewrites' );
  function template_setup_rewrites() {
      add_rewrite_rule('^/custom/?', '^/?template=custom', 'top');
      flush_rewrite_rules(false);
  }
  </code></pre>
  
  <p>For problem 2 I somehow have to make an exception on the rewrite rules generated by Wordpress in order to keep domain-a...</p>
  
  <p>Sorry if it's all a bit vague, but it's complicated to explain.
  ANY help is appreciated!</p>
  
  <p>Thanks</p>