Re: Dumb Apache2 rewrite question

This is a discussion on Re: Dumb Apache2 rewrite question within the Linux Web Servers forums, part of the Web Server and Related Forums category; Stroller wrote: > What I want to do is rewrite any request for a URL of the form > "...


Go Back   Usenet Forums > Web Server and Related Forums > Linux Web Servers

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-01-2003
Klaus Johannes Rusch
 
Posts: n/a
Default Re: Dumb Apache2 rewrite question

Stroller wrote:

> What I want to do is rewrite any request for a URL of the form
> "http://www.somedomain.tld" to "http://somedomain.tld" - just
> stripping off the "www." I could, of course, just make a symlink from
> /home/httpd/www.somedomain.tld to /home/httpd/somedomain.tld (this
> works) but as I add domains this becomes tatty & unmanageable. I think
> it should be pretty easy to do this with rewrite.


Option 1: use directives for virtual hosts, i.e. create a virtual host
somedomain.tld and another host www.somedomain.tld, the latter with a
single Redirect / http://somedomain.tld/ statement

Option 2: use mod_rewrite as you have started already, with one small but
important correction:

> RewriteEngine on
> RewriteCond %{REMOTE_HOST} ^www\.(.*) [NC]
> RewriteRule ^www\.(.*) http://$1 [NC]


The RewriteRule works on the URL portion after the hostname, i.e.

RewriteRule ^/(.*)$ http://somedomain.tld/$1 [L]

> I also want to redirect all requests to "http://mail.some.domain.org"
> to
> "https://mail.some.domain.org" - again, I think this should be pretty
> easy using the same principle.


Again, the same two options, separate virtualhost sections for :80 and
:443, or mod_rewrite as above.

--
Klaus Johannes Rusch
KlausRusch@atmedia.net
http://www.atmedia.net/KlausRusch/


  #2 (permalink)  
Old 09-02-2003
Stroller
 
Posts: n/a
Default Re: Dumb Apache2 rewrite question

Klaus Johannes Rusch <KlausRusch@atmedia.net> wrote in message news:<3F530470.284A7604@atmedia.net>...
> Stroller wrote:
>
> > What I want to do is rewrite any request for a URL of the form
> > "http://www.somedomain.tld" to "http://somedomain.tld" - just
> > stripping off the "www." I could, of course, just make a symlink from
> > /home/httpd/www.somedomain.tld to /home/httpd/somedomain.tld (this
> > works) but as I add domains this becomes tatty & unmanageable. I think
> > it should be pretty easy to do this with rewrite.

>
> Option 1: use directives for virtual hosts, i.e. create a virtual host
> somedomain.tld and another host www.somedomain.tld, the latter with a
> single Redirect / http://somedomain.tld/ statement


Yes, but the more hosts I get, the more untidy it becomes. I have to
add a directive (actually, I can do it just as well with symlinks, I
think) each time I add a host.

> Option 2: use mod_rewrite as you have started already, with one small but
> important correction:
>
> > RewriteEngine on
> > RewriteCond %{REMOTE_HOST} ^www\.(.*) [NC]
> > RewriteRule ^www\.(.*) http://$1 [NC]

>
> The RewriteRule works on the URL portion after the hostname, i.e.
>
> RewriteRule ^/(.*)$ http://somedomain.tld/$1 [L]


Hmmmn... perhaps I didn't make myself clear. Or perhaps I am
misreading your answer. ;-]

I want a rule that simply removes "www." from ANY request for any url
for any virtual host, like this:
http://www.wibble.com => http://wibble.com
http://www.foo.wibble.com => http://foo.wibble.com
http://www.grunt.org => http://grunt.org
http://www.grunt.org/something.bar => http://grunt.org/something.bar
&c &c...

I don't think the example rewrite rule you gave does this, does it..?
Is it possible to use rewrite to alter the domain part of the
request..? I'm sure I read so in the examples.

> > I also want to redirect all requests to "http://mail.some.domain.org"
> > to
> > "https://mail.some.domain.org" - again, I think this should be pretty
> > easy using the same principle.

>
> Again, the same two options, separate virtualhost sections for :80 and
> :443, or mod_rewrite as above.


I have problems with the virtual hosts & port numbers stuff - I'd
rather not go there if I can help it. Since I am learning (I hope!)
rewrite, I'd like to do it that way. Something like:
RewriteEngine on
RewriteCond %{REMOTE_HOST} ^http://www\.(.*) [NC]
RewriteRule ^http://mail.some.domain.org/(.*)$ \
https://mail.some.domain.org/$1 [NC]
????

I thank you for your help an comments, and apologise that I am not
understanding so well,

Stroller.
  #3 (permalink)  
Old 09-02-2003
Joshua Slive
 
Posts: n/a
Default Re: Dumb Apache2 rewrite question

stroller@bigfoot.com (Stroller) wrote in message news:<385cfcc.0309011619.5db77986@posting.google.c om>...

> > > RewriteEngine on
> > > RewriteCond %{REMOTE_HOST} ^www\.(.*) [NC]
> > > RewriteRule ^www\.(.*) http://$1 [NC]

> >
> > The RewriteRule works on the URL portion after the hostname, i.e.
> >
> > RewriteRule ^/(.*)$ http://somedomain.tld/$1 [L]

>
> Hmmmn... perhaps I didn't make myself clear. Or perhaps I am
> misreading your answer. ;-]
>
> I want a rule that simply removes "www." from ANY request for any url
> for any virtual host, like this:


Let's combine the two rules to get something that should work:
RewriteEngine On
RewriteCond %{REMOTE_HOST} ^www\.(.*) [NC]
RewriteRule ^/(.*) http://%1/$1

Note the RewriteRule only matches against the stuff after the hostname,
so we use a back-reference (%1) to grab part of the hostname from the
RewriteCond line.

> > > I also want to redirect all requests to "http://mail.some.domain.org"
> > > to
> > > "https://mail.some.domain.org" - again, I think this should be pretty
> > > easy using the same principle.


> I have problems with the virtual hosts & port numbers stuff - I'd
> rather not go there if I can help it. Since I am learning (I hope!)
> rewrite, I'd like to do it that way. Something like:
> RewriteEngine on
> RewriteCond %{REMOTE_HOST} ^http://www\.(.*) [NC]
> RewriteRule ^http://mail.some.domain.org/(.*)$ \
> https://mail.some.domain.org/$1 [NC]


Again, RewriteRule does not match against the scheme/hostname part.
You can try something like this:

RewriteCond %{HTTPS} !=On
RewriteCond %{REMOTE_HOST} (.*)
RerwiteRule ^/(.*) http://%1/$1

(But I'm not sure, since I don't use SSL myself.)

Joshua.
  #4 (permalink)  
Old 09-02-2003
Joshua Slive
 
Posts: n/a
Default Re: Dumb Apache2 rewrite question

stroller@bigfoot.com (Stroller) wrote in message news:<385cfcc.0309011619.5db77986@posting.google.c om>...

> > > RewriteEngine on
> > > RewriteCond %{REMOTE_HOST} ^www\.(.*) [NC]
> > > RewriteRule ^www\.(.*) http://$1 [NC]

> >
> > The RewriteRule works on the URL portion after the hostname, i.e.
> >
> > RewriteRule ^/(.*)$ http://somedomain.tld/$1 [L]

>
> Hmmmn... perhaps I didn't make myself clear. Or perhaps I am
> misreading your answer. ;-]
>
> I want a rule that simply removes "www." from ANY request for any url
> for any virtual host, like this:


Let's combine the two rules to get something that should work:
RewriteEngine On
RewriteCond %{REMOTE_HOST} ^www\.(.*) [NC]
RewriteRule ^/(.*) http://%1/$1

Note the RewriteRule only matches against the stuff after the hostname,
so we use a back-reference (%1) to grab part of the hostname from the
RewriteCond line.

> > > I also want to redirect all requests to "http://mail.some.domain.org"
> > > to
> > > "https://mail.some.domain.org" - again, I think this should be pretty
> > > easy using the same principle.


> I have problems with the virtual hosts & port numbers stuff - I'd
> rather not go there if I can help it. Since I am learning (I hope!)
> rewrite, I'd like to do it that way. Something like:
> RewriteEngine on
> RewriteCond %{REMOTE_HOST} ^http://www\.(.*) [NC]
> RewriteRule ^http://mail.some.domain.org/(.*)$ \
> https://mail.some.domain.org/$1 [NC]


Again, RewriteRule does not match against the scheme/hostname part.
You can try something like this:

RewriteCond %{HTTPS} !=On
RewriteCond %{REMOTE_HOST} (.*)
RerwiteRule ^/(.*) https://%1/$1

(But I'm not sure, since I don't use SSL myself.)

Joshua.
  #5 (permalink)  
Old 09-02-2003
Thomas Binder
 
Posts: n/a
Default Re: Dumb Apache2 rewrite question

Hi!

Joshua Slive <google@slive.ca> wrote:
> Let's combine the two rules to get something that should work:
>
> RewriteEngine On
> RewriteCond %{REMOTE_HOST} ^www\.(.*) [NC]
> RewriteRule ^/(.*) http://%1/$1
>
> Note the RewriteRule only matches against the stuff after the
> hostname, so we use a back-reference (%1) to grab part of the
> hostname from the RewriteCond line.


But REMOTE_HOST is the _client_ host, not the server instance
accessed. For the latter, you have to use HTTP_HOST (that's
what the client supplied in the Host: header of the request).


Ciao

Thomas


--
Thomas Binder (Gryf @ IRCNet) gryf+usenet@hrzpub.tu-darmstadt.de
PGP-key available on request!
  #6 (permalink)  
Old 09-03-2003
Stroller
 
Posts: n/a
Default Re: Dumb Apache2 rewrite question

gryf+usenet@hrzpub.tu-darmstadt.de (Thomas Binder) wrote in message news:<bj2cml$aas$1@news.tu-darmstadt.de>...
> Hi!
>
> Joshua Slive <google@slive.ca> wrote:
> > Let's combine the two rules to get something that should work:
> >
> > RewriteEngine On
> > RewriteCond %{REMOTE_HOST} ^www\.(.*) [NC]
> > RewriteRule ^/(.*) http://%1/$1
> >
> > Note the RewriteRule only matches against the stuff after the
> > hostname, so we use a back-reference (%1) to grab part of the
> > hostname from the RewriteCond line.

>
> But REMOTE_HOST is the _client_ host, not the server instance
> accessed. For the latter, you have to use HTTP_HOST (that's
> what the client supplied in the Host: header of the request).


Perfect! I did wonder about that, but thought I had seen this syntax
in another example - I was obviously misunderstanding its purpose.

I have edited /etc/apache2/conf/vhosts/dynamic-vhosts.conf thus:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^/(.*) http://%1/$1

Thank you both! This works wonderfully, as you can tell from
http://junk.stroller.uk.eu.org/

Stroller.
  #7 (permalink)  
Old 09-06-2003
Stroller
 
Posts: n/a
Default Re: Dumb Apache2 rewrite question

google@slive.ca (Joshua Slive) wrote in message news:<916ecaf4.0309020713.22bda57c@posting.google. com>...
....
> > > > I also want to redirect all requests to "http://mail.some.domain.org"
> > > > to
> > > > "https://mail.some.domain.org" - again, I think this should be pretty
> > > > easy using the same principle.

>
> > I have problems with the virtual hosts & port numbers stuff - I'd
> > rather not go there if I can help it. Since I am learning (I hope!)
> > rewrite, I'd like to do it that way. Something like:
> > RewriteEngine on
> > RewriteCond %{REMOTE_HOST} ^http://www\.(.*) [NC]
> > RewriteRule ^http://mail.some.domain.org/(.*)$ \
> > https://mail.some.domain.org/$1 [NC]

>
> Again, RewriteRule does not match against the scheme/hostname part.
> You can try something like this:
>
> RewriteCond %{HTTPS} !=On
> RewriteCond %{REMOTE_HOST} (.*)
> RerwiteRule ^/(.*) https://%1/$1


Ah! I think I again did not express myself clearly. I only wish
requests to mail.stroller.uk.eu.org to redirect to https. For any
other domain, http is fine, but I'd like my mail & password (forcibly)
encrypted.

I have tried:

RewriteCond %{HTTPS} !=On
RewriteCond %{HTTP_HOST} ^mail.stroller.uk.eu.org(.*) [NC]
RewriteRule ^/(.*) https://mail.stroller.uk.eu.org$1

But it does not work - I get an error in my browser (Safari on Mac,
but Internet Explorer on Mac also fails) saying that "too many
redirects were encountered accessing this page". It also _seems_ like
I see a lot of network activity on my hub, until I cancel the
operation.

I'm pretty sure that the problem is that my PHP squirrelmail webmail
app itself redirects to the from http://mail.stroller.uk.eu.org to the
login page (I'll leave it accessible so that if you wish you can see).
So I've also tried:

RewriteCond %{HTTPS} !=On
RewriteCond %{HTTP_HOST} ^mail.stroller.uk.eu.org$ [NC]
RewriteRule ^/(.*) https://mail.stroller.uk.eu.org/src/login.php

This seems to redirect to the login page & then fail with the same
message. Which is weird. It seems that after a few attempts with
either of these statements in place, access to my webserver locks up,
but it may be the browser.

Is there some kind of limit on the number of redirects I need to
disable..?

Stroller.
  #8 (permalink)  
Old 09-06-2003
JP. Baker
 
Posts: n/a
Default Re: Dumb Apache2 rewrite question

In article <385cfcc.0309051828.204873b7@posting.google.com> ,
Stroller <stroller@bigfoot.com> wrote:
>RewriteCond %{HTTPS} !=On


The manual doesn't mention HTTPS try

RewriteCond %{SERVER_PORT} !=443

nhoJ
--
John P Baker
  #9 (permalink)  
Old 09-13-2003
Stroller
 
Posts: n/a
Default Re: Dumb Apache2 rewrite question

ccjpb@shark.cse.bris.ac.uk (JP. Baker) wrote in message news:<HKsCpw.2HE@bath.ac.uk>...
> In article <385cfcc.0309051828.204873b7@posting.google.com> ,
> Stroller <stroller@bigfoot.com> wrote:
> >RewriteCond %{HTTPS} !=On

>
> The manual doesn't mention HTTPS try
>
> RewriteCond %{SERVER_PORT} !=443


Sorted!

Sorry to take so long to follow-up to this thread - I've been largely
sulking at things technical this week. Many thanks to everyone who has
posted - I really appreciate your help.

Stroller.
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 04:59 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0