views:

2341

answers:

5

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:

  • http://example.com/terms/ -> http://support.example.com/articles/terms/
  • https://secure.example.com/terms/ -> http://support.example.com/articles/terms/
  • http://dev.example.com/terms/ -> http://support.dev.example.com/articles/terms/
  • https://secure.dev.example.com/terms/ -> http://support.dev.example.com/articles/terms/

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.

+1  A: 

According to this cheatsheet ( http://www.addedbytes.com/download/mod_rewrite-cheat-sheet-v2/png/ ) this should work

RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1

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.

Nicholas
This assumes the rewritten domain will always be www.domain2.com. That is explicitly stated in the question as not being the case.
Sean Carpenter
A: 

If you are staying on the same server then putting this in your .htaccess will work regardless of the server:

RedirectMatch 301 ^/terms$ /articles/terms/

Produces:

http://example.com/terms -> http://example.com/articles/terms

or:

http://test.example.com/terms -> http://test.example.com/articles/terms

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.

If you want:

http://example.com/terms -> http://server02.example.com/articles/terms

Then you'll need to use the HTTP_HOST parameter.

Colonel Sponsz
A: 

You don't need to include this information. Just provide a URI relative to the root.

Redirect temp /terms /articles/terms/

This is explained in the mod_alias documentation:

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.

Jim
A: 

It sounds like what you really need is just an alias?

Alias /terms /www/public/articles/terms/
Jaykul
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.
Jim
A: 

I think you'll want to capture the HTTP_HOST value and then use that in the rewrite rule:

RewriteCond %{HTTP_HOST} (.*)
RewriteRule ^/terms http://support.%1/article/terms [NC,R=302]
Sean Carpenter