<p>I'm trying to redirect requests for a wildcard domain to a sub-directory.<br />
ie. <code>something.blah.example.com</code> --&gt; <code>blah.example.com/something</code></p>
<p>I don't know how to get the subdomain name to use in the rewrite rule.</p>
<p><strong>Final Solution:</strong></p>
<pre><code>RewriteCond %{HTTP_HOST} !^blah\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)
RewriteRule ^(.*) /%1/$1 [L]
</code></pre>
<p>Or as pointed out by pilif</p>
<pre><code>RewriteCond %{HTTP_HOST} ^([^.]+)\.blah\.example\.com$
</code></pre>

[{'answer_id': 49504, 'author': 'pilif', 'author_id': 5083, 'author_profile': 'https://Stackoverflow.com/users/5083', 'pm_score': 6, 'selected': True, 'text': '<p>You should have a look at the <a href="http://httpd.apache.org/docs/2.2/rewrite/" rel="noreferrer" title="URL Rewriting Guide">URL Rewriting Guide</a> from the apache documentation.</p>\n\n<p>The following is untested, but it should to the trick:</p>\n\n<pre><code>RewriteCond %{HTTP_HOST} ^([^.]+)\\.blah\\.domain\\.com$\nRewriteRule ^/(.*)$           http://blah.domain.com/%1/$1 [L,R] \n</code></pre>\n\n<p>This only works if the subdomain contains no dots. Otherwise, you\'d have to alter the Regexp in RewriteCond to match any character which should still work due to the anchoring, but this certainly feels safer.</p>\n'}, {'answer_id': 49505, 'author': 'BlaM', 'author_id': 999, 'author_profile': 'https://Stackoverflow.com/users/999', 'pm_score': 2, 'selected': False, 'text': "<p>Try this:</p>\n\n<pre><code>RewriteCond %{HTTP_HOST} (.+)\\.blah\\.domain\\.com\nRewriteRule ^(.+)$ /%1/$1 [L]\n</code></pre>\n\n<p>@pilif (see comment): Okay, that's true. I just copied a .htaccess that I use on one of my projects. Guess it has a slightly different approach :)</p>\n"}, {'answer_id': 49838, 'author': 'pilif', 'author_id': 5083, 'author_profile': 'https://Stackoverflow.com/users/5083', 'pm_score': 1, 'selected': False, 'text': '<p>@Sam</p>\n\n<p>your RewriteCond line is wrong. The expansion of the variable is triggered with %, not $.</p>\n\n<pre><code>RewriteCond %{HTTP_HOST} ^([^\\.]+)\\.media\\.xnet\\.tk$\n            ^\n</code></pre>\n\n<p>that should do the trick</p>\n'}]
['https://Stackoverflow.com/questions/49500', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/428/']