You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Robert Moskowitz <rg...@htt-consult.com> on 2017/03/05 20:57:11 UTC

[users@httpd] Help with rewriterule

Hello,

This is for Apache 2.4 on Centos7.  I am migrating from Apache 2.2 on 
Centos6.

I want to support redirecting all webmail queries to https.  for either 
queries to:

webmail.foo.com or foo.com/webmail

My old rules were:

<VirtualHost *:80>
     ServerName webmail.htt-consult.com
     ServerAlias webmail
     RewriteEngine On
         RewriteCond  %{SERVER_PORT} !^443$
         RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
         ExpiresDefault "access plus 10 years"
         AddOutputFilterByType DEFLATE text/html text/plain text/xml
     php_admin_flag session.cookie_secure "1"
</VirtualHost>

This SEEMED to only work for the webmail virtual host, and not others on 
this server.  When I used this rule set on the new server, it clearly 
was redirecting all web accesses to https.  So I tried to write a more 
restrictive rule, trying to follow instructions from

http://httpd.apache.org/docs/current/rewrite/intro.html

It would seem the rule:

         RewriteRule ^.*webmail https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

Should work, but the test site I tried:

http://martinmelin.se/rewrite-rule-tester/

Did not show this did the rewrite to https.  Also how to direct 
Webmail.foo.com to https://webmail.foo.com?

Thank you



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Help with rewriterule

Posted by Luca Toscano <to...@gmail.com>.
2017-03-06 19:15 GMT+01:00 Robert Moskowitz <rg...@htt-consult.com>:

> Luca,
>
> Thank you for replying.
>
> On 03/06/2017 04:07 AM, Luca Toscano wrote:
>
> Hi Robert,
>
> 2017-03-05 21:57 GMT+01:00 Robert Moskowitz <rg...@htt-consult.com>:
>
>> Hello,
>>
>> This is for Apache 2.4 on Centos7.  I am migrating from Apache 2.2 on
>> Centos6.
>>
>> I want to support redirecting all webmail queries to https.  for either
>> queries to:
>>
>> webmail.foo.com or foo.com/webmail
>>
>> My old rules were:
>>
>> <VirtualHost *:80>
>>     ServerName webmail.htt-consult.com
>>     ServerAlias webmail
>>     RewriteEngine On
>>         RewriteCond  %{SERVER_PORT} !^443$
>>         RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
>>         ExpiresDefault "access plus 10 years"
>>         AddOutputFilterByType DEFLATE text/html text/plain text/xml
>>     php_admin_flag session.cookie_secure "1"
>> </VirtualHost>
>>
>> This SEEMED to only work for the webmail virtual host, and not others on
>> this server.  When I used this rule set on the new server, it clearly was
>> redirecting all web accesses to https.
>
>
> Have you checked if the VirtualHost is the default one (you can use
> apachectl -S to confirm)?
>
>
> Comes back <null> response.
>


This is really strange.. Have you checked if the apachectl binary is
working correctly?


>
>
>> So I tried to write a more restrictive rule, trying to follow
>> instructions from
>>
>> http://httpd.apache.org/docs/current/rewrite/intro.html
>>
>> It would seem the rule:
>>
>>         RewriteRule ^.*webmail https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
>>
>> Should work, but the test site I tried:
>>
>> http://martinmelin.se/rewrite-rule-tester/
>>
>> Did not show this did the rewrite to https.
>
>
> So from http://httpd.apache.org/docs/current/mod/mod_rewrite.
> html#rewriterule:
>
> "RewriteRule Pattern Substitution [flags]"
> "In VirtualHost context, The Pattern will initially be matched against the
> part of the URL after the hostname and port, and before the query string
> (e.g. "/app1/index.html"). This is the (%-decoded) URL-path."
>
> If I got it correctly you are trying to match the hostname in the Pattern,
> that shouldn't work in Vhost context.
>
> Also how to direct Webmail.foo.com to https://webmail.foo.com?
>>
>
> https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect or
> https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirectmatch might
> help you (and in my opinion they are way more maintainable than rewrite
> rules).
>
>
>
> RedirectMatch ^.*webmail https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
>
> But how do I get this NOT to redirect when it is already https:// ?
>

It shouldn't be a problem if you use the rule only in the Virtual Host
listening on port 80 (since no https request should land there).


> And how do I handle Webmail as well as webmail?  Would that be
>
> RedirectMatch ^.*[wW]ebmail https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
>

I would do multiple tests on your environment to see which one works best :)

Thanks!

Luca

Re: [users@httpd] Help with rewriterule

Posted by Robert Moskowitz <rg...@htt-consult.com>.
Luca,

Thank you for replying.

On 03/06/2017 04:07 AM, Luca Toscano wrote:
> Hi Robert,
>
> 2017-03-05 21:57 GMT+01:00 Robert Moskowitz <rgm@htt-consult.com 
> <ma...@htt-consult.com>>:
>
>     Hello,
>
>     This is for Apache 2.4 on Centos7.  I am migrating from Apache 2.2
>     on Centos6.
>
>     I want to support redirecting all webmail queries to https.  for
>     either queries to:
>
>     webmail.foo.com <http://webmail.foo.com> or foo.com/webmail
>     <http://foo.com/webmail>
>
>     My old rules were:
>
>     <VirtualHost *:80>
>         ServerName webmail.htt-consult.com
>     <http://webmail.htt-consult.com>
>         ServerAlias webmail
>         RewriteEngine On
>             RewriteCond  %{SERVER_PORT} !^443$
>             RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
>             ExpiresDefault "access plus 10 years"
>             AddOutputFilterByType DEFLATE text/html text/plain text/xml
>         php_admin_flag session.cookie_secure "1"
>     </VirtualHost>
>
>     This SEEMED to only work for the webmail virtual host, and not
>     others on this server.  When I used this rule set on the new
>     server, it clearly was redirecting all web accesses to https. 
>
>
> Have you checked if the VirtualHost is the default one (you can use 
> apachectl -S to confirm)?

Comes back <null> response.

>     So I tried to write a more restrictive rule, trying to follow
>     instructions from
>
>     http://httpd.apache.org/docs/current/rewrite/intro.html
>     <http://httpd.apache.org/docs/current/rewrite/intro.html>
>
>     It would seem the rule:
>
>             RewriteRule ^.*webmail
>     https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
>
>     Should work, but the test site I tried:
>
>     http://martinmelin.se/rewrite-rule-tester/
>     <http://martinmelin.se/rewrite-rule-tester/>
>
>     Did not show this did the rewrite to https. 
>
>
> So from 
> http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule:
>
> "RewriteRule Pattern Substitution [flags]"
> "In VirtualHost context, The Pattern will initially be matched against 
> the part of the URL after the hostname and port, and before the query 
> string (e.g. "/app1/index.html"). This is the (%-decoded) URL-path."
> If I got it correctly you are trying to match the hostname in the 
> Pattern, that shouldn't work in Vhost context.
>
>     Also how to direct Webmail.foo.com <http://Webmail.foo.com> to
>     https://webmail.foo.com?
>
>
> https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect or 
> https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirectmatch 
> might help you (and in my opinion they are way more maintainable than 
> rewrite rules).


RedirectMatch ^.*webmail https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

But how do I get this NOT to redirect when it is already https:// ?
And how do I handle Webmail as well as webmail?  Would that be

RedirectMatch ^.*[wW]ebmail https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

thanks


Re: [users@httpd] Help with rewriterule

Posted by Luca Toscano <to...@gmail.com>.
Hi Robert,

2017-03-05 21:57 GMT+01:00 Robert Moskowitz <rg...@htt-consult.com>:

> Hello,
>
> This is for Apache 2.4 on Centos7.  I am migrating from Apache 2.2 on
> Centos6.
>
> I want to support redirecting all webmail queries to https.  for either
> queries to:
>
> webmail.foo.com or foo.com/webmail
>
> My old rules were:
>
> <VirtualHost *:80>
>     ServerName webmail.htt-consult.com
>     ServerAlias webmail
>     RewriteEngine On
>         RewriteCond  %{SERVER_PORT} !^443$
>         RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
>         ExpiresDefault "access plus 10 years"
>         AddOutputFilterByType DEFLATE text/html text/plain text/xml
>     php_admin_flag session.cookie_secure "1"
> </VirtualHost>
>
> This SEEMED to only work for the webmail virtual host, and not others on
> this server.  When I used this rule set on the new server, it clearly was
> redirecting all web accesses to https.


Have you checked if the VirtualHost is the default one (you can use
apachectl -S to confirm)?


> So I tried to write a more restrictive rule, trying to follow instructions
> from
>
> http://httpd.apache.org/docs/current/rewrite/intro.html
>
> It would seem the rule:
>
>         RewriteRule ^.*webmail https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
>
> Should work, but the test site I tried:
>
> http://martinmelin.se/rewrite-rule-tester/
>
> Did not show this did the rewrite to https.


So from
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule:

"RewriteRule Pattern Substitution [flags]"
"In VirtualHost context, The Pattern will initially be matched against the
part of the URL after the hostname and port, and before the query string
(e.g. "/app1/index.html"). This is the (%-decoded) URL-path."

If I got it correctly you are trying to match the hostname in the Pattern,
that shouldn't work in Vhost context.

Also how to direct Webmail.foo.com to https://webmail.foo.com?
>

https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect or
https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirectmatch might
help you (and in my opinion they are way more maintainable than rewrite
rules).

Hope that helps!

Luca