.htaccess: append subdomain as query to existing queries
<p>I'm trying to append the subdomain as a query to eventual existing queries using htaccess.</p>

<p><code>http://test.domain.com</code> should be <code>http://test.domain.com?x=test</code>
<code>http://test.domain.com?id=1</code> should be <code>http://test.domain.com?id=1&amp;x=test</code></p>

<p>This is what I have done, but it doesn't work and I can figure out why:</p>

<pre><code>RewriteCond %{HTTP_HOST} ^([a-z0-9_-]+)\.domain\.com$ [NC]
// exclude www.domain.com
RewriteCond %1 !^(www)$ [NC]
RewriteRule ^[^\?]*(?:\?(.*))?$ index.php?$1&amp;x=%1 [L]
</code></pre>

<p>My understanding was</p>

<p><code>[^\?]*</code> all characters except ?, match 0 or more times</p>

<p><code>(?:</code> start of a non capturing group</p>

<p><code>\?</code> match ? literally</p>

<p><code>(.*)</code> all characters after ? as a group</p>

<p><code>)?</code> end of the non capturing group, match 0 or 1 times</p>

<p>But it does not work. Where is my mistake?</p>

<p><strong>UPDATE 1:</strong></p>

<p>I could make it work by using the following rule</p>

<pre><code>RewriteRule (.*) index.php?$1&amp;x=%1 [QSA,L]
</code></pre>

<p><code>http://test1.domain.com?y=test1</code> brings me <code>[x=&gt;test1,y=&gt;test2]</code></p>

<p>but </p>

<p><code>http://test1.domain.com?y=test1&amp;x=test3</code> brings me <code>[x=&gt;test3,y=&gt;test2]</code></p>

<p>So it overrides my x value. Is there a way to block that?</p>

<p><strong>UPDATE 2</strong></p>

<p>This is the code I'm using now:
RewriteEngine On </p>

<pre><code>RewriteCond %{HTTP_HOST} ^(?!www)([\w-]+)\. [NC]
RewriteCond %1::%{QUERY_STRING} !^(.+?)::x=\1(?:&amp;|$) [NC]
RewriteRule ^ index.php?%{QUERY_STRING}&amp;x=%1 [L]
</code></pre>