httpd-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Igor Galić <i.ga...@brainsware.org>
Subject Re: [users@httpd] mod_rewrite help?
Date Sat, 03 Dec 2011 12:29:53 GMT


----- Original Message -----
> 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

Yes, That is very true.
And it's easier yet to not use mod_rewrite to begin with.

> 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 only problem with RewriteRule / is that it won't work, e.g.:

igalic@tynix ~ % echo f/oo | perl -ne 'print if m{/}'
f/oo

That is not the desired effect.

It's much easier and cleaner to achieve with VHosts, because it's
actually readable.

# Redirect Only VHost
<VirtualHost *:80>
   ServerName example.com
   Redirect / http://www.example.com/blog
</VirtualHost>

<VirtualHost *:80>
  ServerName www.example.com
# No looping here, we only redirect /
  RedirectMatch ^/$ http://www.example.com/blog

# I shall leave the setup of a DocumentRoot as
# an exercise to the reader.
</VirtualHost>


Now, the tricky part is HTTPS, but only because for what you want
you'll need two valid certificates. (Or one valid for both domains
via SubjectAltName. Not sure what's more expensive). But essentially
it looks the same with *:443 and https:// in the Redirects.

-- 
Igor Galić

Tel: +43 (0) 664 886 22 883
Mail: i.galic@brainsware.org
URL: http://brainsware.org/
GPG: 6880 4155 74BD FD7C B515  2EA5 4B1D 9E08 A097 C9AE


---------------------------------------------------------------------
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


Mime
View raw message