[prev in list] [next in list] [prev in thread] [next in thread] 

List:       apache-httpd-users
Subject:    Re: [users@httpd] mod_rewrite help?
From:       Mark Montague <mark () catseye ! org>
Date:       2011-11-30 15:01:59
Message-ID: 4ED64567.1030108 () catseye ! org
[Download RAW message or body]

On November 29, 2011 20:08 , <jtp@nc.rr.com> wrote:
> I'm having issues with mod_rewrite and Apache. Hopefully someone can
> offer some tips / help.
>
> I'm looking for something simple:
>
> (a) http://www.website.com ->  http://www.website.com/blog
> (b) http://website.com ->  http://www.website.com/blog
>
> Also:
> (1) https://www.website.com ->  https://www.website.com/blog
> (2) https://website.com ->  https://www.website.com/blog
>
> (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 www.website.com.

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

* You have a virtual host, V3, that serves HTTPS traffic for the 
www.website.com

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

Under these assumptions,

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

RewriteRule / http://www.website.com/blog

(b) can be phrased as "when a user requests any resource beneath 
http://website.com/ I want the URL rewritten to request the same 
resource under http://www.website.com/.  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 / http://www.website.com/

Note that if a user goes to http://website.com/ they will first be 
redirected to http://www.website.com/ and then they will be redirected 
again to http://www.website.com/blog.  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
   mark@catseye.org


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic