httpd-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Montague <>
Subject Re: [users@httpd] mod_rewrite help?
Date Wed, 30 Nov 2011 15:01:59 GMT
On November 29, 2011 20:08 , <> wrote:
> I'm having issues with mod_rewrite and Apache. Hopefully someone can
> offer some tips / help.
> I'm looking for something simple:
> (a) ->
> (b) ->
> Also:
> (1) ->
> (2) ->
> (note that HTTP is NOT redirecting to HTTPS)

It will be easier and cleaner if you put your rewrite rules in your 
httpd configuration file(s) instead of in .htaccess.  This is 
particularly true if you have several virtual hosts being served from 
the same DocumentRoot.

The answer to your question depends on how you have your virtual hosts 
set up.  Let's assume the following:

* You have one virtual host, let's call it V1 for the purpose of 
referring to it in this email, that serves HTTP traffic for

* You have another virtual host, V2, that serves HTTP traffic for the 
site  You do NOT have site up as a ServerAlias 
for in the virtual host V1.

* You have a virtual host, V3, that serves HTTPS traffic for the

* You have a virtual host, V4, that serves HTTPS traffic for

Under these assumptions,

(a) can be phrased as "when a user goes to I 
want them to be redirected to  All other 
URL paths for the site, other than /, should be unaffected -- for 
example, if the user goes to then the URL 
should remain unchanged".  The mod_rewrite rule that does this, which 
should be placed in virtual host V1, is:

RewriteRule /

(b) can be phrased as "when a user requests any resource beneath I want the URL rewritten to request the same 
resource under  It's best to use the Redirect 
directive for this instead of a mod_rewrite rule.  The Redirect 
directive should be placed in virtual host V2:

Redirect /

Note that if a user goes to they will first be 
redirected to and then they will be redirected 
again to  If you try to do the redirect in 
a single step, then this complicates URL handling, so I don't recommend 
it in general.

(1) and (2) use the same directives as (a) and (b), but you put them in 
the virtual host contexts for V3 and V4, respectively, instead of V1 and V2.

Note that the directives should be placed directly in the appropriate 
virtual host context, and they should not be in a Directory or other 
context within the virtual host context.

   Mark Montague

The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:> for more info.
To unsubscribe, e-mail:
   "   from the digest:
For additional commands, e-mail:

View raw message