<p><strong>Problem</strong></p>

<p>I need to redirect some short convenience URLs to longer actual URLs. The site in question uses a set of subdomains to identify a set of development or live versions.</p>

<p>I would like the URL to which certain requests are redirected to include the HTTP_HOST such that I don't have to create a custom .htaccess file for each host.</p>

<p><strong>Host-specific Example (snipped from .htaccess file)</strong></p>

<pre><code>Redirect /terms http://support.dev01.example.com/articles/terms/
</code></pre>

<p>This example works fine for the development version running at dev01.example.com. If I use the same line in the main .htaccess file for the development version running under dev02.example.com I'd end up being redirected to the wrong place.</p>

<p><strong>Ideal rule (not sure of the correct syntax)</strong></p>

<pre><code>Redirect /terms http://support.{HTTP_HOST}/articles/terms/
</code></pre>

<p>This rule does not work and merely serves as an example of what I'd like to achieve. I could then use the exact same rule under many different hosts and get the correct result.</p>

<p><strong>Answers?</strong></p>

<ul>
<li>Can this be done with mod_alias or does it require the more complex mod_rewrite?</li>
<li>How can this be achieved using mod_alias or mod_rewrite? I'd prefer a mod_alias solution if possible.</li>
</ul>

<p><strong>Clarifications</strong></p>

<p>I'm not staying on the same server. I'd like:</p>

<ul>
<li>http://<strong>example.com</strong>/terms/ -> <a href="http://support" rel="nofollow noreferrer">http://support</a>.<strong>example.com</strong>/articles/terms/</li>
<li><a href="https://secure" rel="nofollow noreferrer">https://secure</a>.<strong>example.com</strong>/terms/ -> <a href="http://support" rel="nofollow noreferrer">http://support</a>.<strong>example.com</strong>/articles/terms/</li>
<li>http://<strong>dev.example.com</strong>/terms/ -> <a href="http://support" rel="nofollow noreferrer">http://support</a>.<strong>dev.example.com</strong>/articles/terms/</li>
<li><a href="https://secure" rel="nofollow noreferrer">https://secure</a>.<strong>dev.example.com</strong>/terms/ -> <a href="http://support" rel="nofollow noreferrer">http://support</a>.<strong>dev.example.com</strong>/articles/terms/</li>
</ul>

<p>I'd like to be able to use the same rule in the .htaccess file on both example.com and dev.example.com. In this situation I'd need to be able to refer to the HTTP_HOST as a variable rather than specifying it literally in the URL to which requests are redirected.</p>

<p>I'll investigate the HTTP_HOST parameter as suggested but was hoping for a working example.</p>

[{'answer_id': 72530, 'author': 'Nicholas', 'author_id': 8054, 'author_profile': 'https://Stackoverflow.com/users/8054', 'pm_score': -1, 'selected': False, 'text': '<p>According to this cheatsheet ( <a href="http://www.addedbytes.com/download/mod_rewrite-cheat-sheet-v2/png/" rel="nofollow noreferrer">http://www.addedbytes.com/download/mod_rewrite-cheat-sheet-v2/png/</a> ) this should work</p>\n\n<pre><code>RewriteCond %{HTTP_HOST} ^www\\.domain\\.com$ [NC]\nRewriteRule ^(.*)$ http://www.domain2.com/$1\n</code></pre>\n\n<p>Note that i don\'t have a way to test this so this should be taken as a pointer in the right direction as opposed to an explicit answer.</p>\n'}, {'answer_id': 72597, 'author': 'Colonel Sponsz', 'author_id': 11651, 'author_profile': 'https://Stackoverflow.com/users/11651', 'pm_score': -1, 'selected': False, 'text': "<p>If you are staying on the same server then putting this in your .htaccess will work regardless of the server:</p>\n\n<pre><code>RedirectMatch 301 ^/terms$ /articles/terms/\n</code></pre>\n\n<p>Produces:</p>\n\n<pre><code>http://example.com/terms -&gt; http://example.com/articles/terms\n</code></pre>\n\n<p>or:</p>\n\n<pre><code>http://test.example.com/terms -&gt; http://test.example.com/articles/terms\n</code></pre>\n\n<p>Obviously you'll need to adjust the REGEX matching and the like to make sure it copes with what you are going to throw at it.  Same goes for the 301, you might want a 302 if you don't want browsers to cache the redirect.</p>\n\n<p>If you want:</p>\n\n<pre><code>http://example.com/terms -&gt; http://server02.example.com/articles/terms\n</code></pre>\n\n<p>Then you'll need to use the HTTP_HOST parameter.</p>\n"}, {'answer_id': 72652, 'author': 'Jim', 'author_id': 8427, 'author_profile': 'https://Stackoverflow.com/users/8427', 'pm_score': -1, 'selected': False, 'text': '<p>You don\'t need to include this information.  Just provide a URI relative to the root.</p>\n\n<pre><code>Redirect temp /terms /articles/terms/\n</code></pre>\n\n<p>This is explained in <a href="http://httpd.apache.org/docs/2.2/mod/mod_alias.html#redirect" rel="nofollow noreferrer">the mod_alias documentation</a>:</p>\n\n<blockquote>\n  <p>The new URL should be an absolute URL beginning with a scheme and hostname, but a URL-path beginning with a slash may also be used, in which case the scheme and hostname of the current server will be added.</p>\n</blockquote>\n'}, {'answer_id': 72707, 'author': 'Jaykul', 'author_id': 8718, 'author_profile': 'https://Stackoverflow.com/users/8718', 'pm_score': -1, 'selected': False, 'text': '<p>It sounds like what you really need is just an alias?</p>\n\n<pre><code>Alias /terms /www/public/articles/terms/\n</code></pre>\n'}, {'answer_id': 72828, 'author': 'Sean Carpenter', 'author_id': 729, 'author_profile': 'https://Stackoverflow.com/users/729', 'pm_score': 0, 'selected': False, 'text': "<p>I think you'll want to capture the HTTP_HOST value and then use that in the rewrite rule:  </p>\n\n<pre><code>RewriteCond %{HTTP_HOST} (.*)\nRewriteRule ^/terms http://support.%1/article/terms [NC,R=302]\n</code></pre>\n"}, {'answer_id': 10010128, 'author': 'jornare', 'author_id': 1249477, 'author_profile': 'https://Stackoverflow.com/users/1249477', 'pm_score': 0, 'selected': False, 'text': '<p>If I understand your question right, you want a 301 redirect (tell browser to go to other URL).\nIf my solution is not the correct one for you, try this tool: <a href="http://www.htaccessredirect.net/index.php" rel="nofollow">http://www.htaccessredirect.net/index.php</a> and figure out what works for you.</p>\n\n<pre><code>//301 Redirect Entire Directory\nRedirectMatch 301 /terms(.*) /articles/terms/$1\n\n//Change default directory page\nDirectoryIndex \n</code></pre>\n'}, {'answer_id': 10024434, 'author': 'Olivier Pons', 'author_id': 106140, 'author_profile': 'https://Stackoverflow.com/users/106140', 'pm_score': 3, 'selected': True, 'text': '<p>It\'s strange that nobody has done the actual <strong>working</strong> answer (lol):</p>\n\n<pre><code>RewriteCond %{HTTP_HOST} support\\.(([^\\.]+))\\.example\\.com\nRewriteRule ^/terms http://support.%1/article/terms [NC,QSA,R]\n</code></pre>\n\n<hr>\n\n<p>To help you doing the job faster, my favorite tool to check for regexp:</p>\n\n<p><a href="http://www.quanetic.com/Regex" rel="nofollow">http://www.quanetic.com/Regex</a> (don\'t forget to choose ereg(POSIX) instead of preg(PCRE)!)</p>\n\n<p>You use this tool when you want to check the URL and see if they\'re valid or not.</p>\n'}]
['https://Stackoverflow.com/questions/72458', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5343/']