How do I use .htaccess to redirect to a URL containing HTTP_HOST?

16,146

It's strange that nobody has done the actual working answer (lol):

RewriteCond %{HTTP_HOST} support\.(([^\.]+))\.example\.com
RewriteRule ^/terms http://support.%1/article/terms [NC,QSA,R]

To help you doing the job faster, my favorite tool to check for regexp:

http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

You use this tool when you want to check the URL and see if they're valid or not.

Share:
16,146
Jon Cram
Author by

Jon Cram

I'm Jon Cram, a software developer and interface designer living with my wife and cat on a boat near London (UK) in a house in Cardiff. I work for Box UK Lime Green Tangerine myself on a new startup. I work mostly with web apps, developing in PHP for quick-and-easy bits and Java for heavy-duty bits. I recently discovered that Grails is the best thing ever. I own and run Hosting Reborn, the UK's first pay-as-you-go web hosting service.

Updated on June 05, 2022

Comments

  • Jon Cram
    Jon Cram about 2 years

    Problem

    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.

    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.

    Host-specific Example (snipped from .htaccess file)

    Redirect /terms http://support.dev01.example.com/articles/terms/
    

    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.

    Ideal rule (not sure of the correct syntax)

    Redirect /terms http://support.{HTTP_HOST}/articles/terms/
    

    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.

    Answers?

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

    Clarifications

    I'm not staying on the same server. I'd like:

    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.

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

  • Lomithrani
    Lomithrani almost 16 years
    Alias is suboptimal because it doesn't perform a redirect. That means that you have two different resources without a canonical URI, which causes various problems - e.g. less effective caching, malfunctioning browser history, etc.
  • Sean Carpenter
    Sean Carpenter about 15 years
    This assumes the rewritten domain will always be www.domain2.com. That is explicitly stated in the question as not being the case.