homepage Welcome to WebmasterWorld Guest from 54.226.230.76
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
redirect to different subdomain with mod rewrite on MacOS
mod_rewrite works in one case and doesn't in the other with the same syntax
quwax




msg:4648851
 8:48 am on Feb 25, 2014 (gmt 0)

Hello,
I'm not really an expert for this and ran into this problem with mod_rewrite on a MacOS 10.6 Server.
The 0002_any_80_example.com.conf file looks like this:
RewriteEngine On
this works:
RewriteCond %{HTTP_HOST} ^xx\.(.*) [OR,nocase]
RewriteCond %{HTTP_HOST} ^yy\.(.*) [OR,nocase]
RewriteCond %{HTTP_HOST} ^nn\.(.*) [nocase]
RewriteRule ^/?(.*) https://nn.example.com:8991%{REQUEST_URI} [L,R=permanent]
and this doesn't work:
RewriteCond %{HTTP_HOST} ^mail\.(.*) [nocase]
RewriteRule ^/?(.*) https://mail.example.com/webmail/ [L,R=permanent]

in the mod_alias I have, which works:
RedirectMatch permanent /webmail* https://mail.example.com/webmail$1

The 0001_any_443_example.com.conf file looks like this and it all works.
RewriteCond %{HTTP_HOST} ^xx\.(.*) [OR,nocase]
RewriteCond %{HTTP_HOST} ^yy\.(.*) [OR,nocase]
RewriteCond %{HTTP_HOST} ^nn\.(.*) [nocase]
RewriteRule ^/?(.*) https://nn.example.com:8991%{REQUEST_URI} [L,R=permanent]
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^mail\.example\.com/webmail.* [nocase]
RewriteRule .* - [L]
RewriteCond %{REQUEST_URI} ^/webmail.* [nocase]
RewriteRule ^/?(.*) https://mail.example.com/$1 [L,R=permanent]

RewriteCond %{HTTP_HOST} ^mail\.(.*) [nocase]
RewriteRule ^/?(.*) https://mail.example.com/webmail/ [L,R=permanent]

So this is confusing to me
What I want to achieve is that any subdomain starting with xx or yy or nn gets redirected to a different port and that everything which has webmail inside or the subdomain starts with mail gets redirected to the webmail-interface on the subdomain mail.example.com
Obviously I'm missing something here any help would be highly appreciated
thanks
quwax

 

g1smd




msg:4648856
 9:20 am on Feb 25, 2014 (gmt 0)

Uncaptured leading or trailing .* are redundant and can be removed.

Your RedirectMatch rule has captured nothing to populate $1 with.

Convert all Redirect and RedirectMatch directives to RewriteRule syntax.

RewriteCond %{HTTP_HOST} ^xx\.(.*) [OR,nocase]
RewriteCond %{HTTP_HOST} ^yy\.(.*) [OR,nocase]
RewriteCond %{HTTP_HOST} ^nn\.(.*) [nocase]

simplifies to

RewriteCond %{HTTP_HOST} ^(nnxxyy)\. [NC]

Remove all of the question marks, everywhere.

Add a blank line after every rule for clarity.

Add a comment before each rule describing in plain English what it is supposed to do.

As currently coded, requesting nn.example.com will lead to an infinite loop of redirects.

phranque




msg:4649057
 8:09 pm on Feb 25, 2014 (gmt 0)

"ran into a problem" and "this doesn't work" is not a sufficient technical description of what the server response was.

quwax




msg:4649150
 1:25 am on Feb 26, 2014 (gmt 0)

Hello,
thanks a lot, I was sure, there must be an easier way to write the "or" condition :-)
Is there also something like that like:
if ({HTTP_HOST} == ^mail\.) | ({REQUEST_URI} == ^/webmail)
do something?

one thing is still not yet working:

And this how the http config file looks like now:
# redirect evey subdomain xx, yy nn -> nn on port 8991 with SSL (OK, working)
RewriteCond %{HTTP_HOST} ^(xxyynn)\. [NC]
RewriteRule .* https://nn.example.com:8991%{REQUEST_URI} [L,R=permanent]

#redirect any subdomain starting with mail -> mail.example.com/webmail/ with SSL (NotOK, not working)
RewriteCond %{HTTP_HOST} ^mail\. [NC]
RewriteRule .* https://mail.example.com/webmail/ [L,R=permanent]

#redirect anything containing webmail -> mail.example.com/webmail/ with SSL (OK, working)
RewriteCond %{REQUEST_URI} ^/webmail. [NC]
RewriteRule .* https://mail.example.com/$1 [L,R=permanent]

and this is the https config (all is working as expected):
# redirect evey subdomain xx, yy nn -> nn on port 8991 with SSL
RewriteCond %{HTTP_HOST} ^(xxyynn)\. [NC]
RewriteRule .* https://nn.example.com:8991%{REQUEST_URI} [L,R=permanent]

# pass through anything with https://mail.example.com/webmail/
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^mail\.example\.com/webmail. [NC]
RewriteRule .* - [L]

#redirect anything containing webmail -> mail.example.com/webmail/ with SSL
RewriteCond %{REQUEST_URI} ^/webmail. [NC]
RewriteRule .* https://mail.example.com/$1 [L,R=permanent]

#redirect any subdomain starting with mail -> mail.example.com/webmail/ with SSL
RewriteCond %{HTTP_HOST} ^mail\. [NC]
RewriteRule .* https://mail.example.com/webmail/ [L,R=permanent]


The only thing not working is this one in the http (port 80) config file:
RewriteCond %{HTTP_HOST} ^mail\. [NC]
RewriteRule .* https://mail.example.com/webmail/ [L,R=permanent]

the same thing works in the SSL (port 443) config file and makes my head itch ;-)

thanks again
quwax

[edited by: quwax at 1:30 am (utc) on Feb 26, 2014]

quwax




msg:4649151
 1:27 am on Feb 26, 2014 (gmt 0)

phranque,
yes correct, the server sends me the normal index page through port 80, instead of redirecting to the webmail interface on port 443.
cheers
quwax

lucy24




msg:4649172
 2:59 am on Feb 26, 2014 (gmt 0)

RewriteCond %{HTTP_HOST} ^mail\. [NC]

Does this actually occur? RewriteRules apply only to http requests. Is this (or was this) a public subdomain?

Is there also something like that like:
if ({HTTP_HOST} == ^mail\.) | ({REQUEST_URI} == ^/webmail)

Possibly in 2.4, which has a vastly expanded <If> capacity. In mod_rewrite in 2.[02] you're looking at

RewriteCond %{HTTP_HOST} ^mail\. [OR]
RewriteCond %{REQUEST_URI} ^/webmail

.. except that I'm not sure you'd want to, because mod_rewrite works most speedily if you're not evaluating conditions at all. So that means one conditionless rule for

RewriteRule ^webmail et cetera

and a different one, with condition, for the "mail.example.com" requests. Can we hope that nobody has yet asked for mail.example.com/webmail/ ?

quwax




msg:4649198
 6:16 am on Feb 26, 2014 (gmt 0)

Hello,
thanks. Yes, mail.example.com is a valid subdomain and I want to redirect any traffic of that subdomain from http to https and the webmail-interface.
If the subdomain is called like that:
mail.example.com/webmail
there's one rule in the http config, which redirects that traffic to https and in the https part there's one rule, which just skips anything like that:
# pass through anything with https://mail.example.com/webmail/
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^mail\.example\.com/webmail. [NC]
RewriteRule .* - [L]

the problem I have is that this rule in the http part is not working, the server sends me back the index.html file instead of redirecting the traffic to SSL etc.

RewriteCond %{HTTP_HOST} ^mail\. [NC]
RewriteRule (.*) https://mail.example.com/webmail/ [L,R=permanent]

the rules in the chain before are working properly and have the same syntax:
RewriteCond %{HTTP_HOST} ^(xxyynn)\. [NC]
RewriteRule (.*) https://nn.example.com:8991%{REQUEST_URI} [L,R=permanent]

they are also working as expected in the SSL part and therefore I don't really understand it.
I also don't see anything in the log. It's like the rule is not there.
I also tried this without success:
RewriteCond %{HTTP_HOST} ^mail\. [OR,NC]
RewriteCond %{REQUEST_URI} ^/webmail. [NC]
RewriteRule (.*) https://mail.example.com/webmail/ [L,R=permanent]

I just don't get what I'm doing wrong here

thanks
quwax

quwax




msg:4649588
 6:27 am on Feb 27, 2014 (gmt 0)

Hello again,
so it looks like the harddisk went bye bye. First Apache started crashing after a restart, telling me there's no SSL certificate, which was there of course and then some i/o errors popped up in the sys log. And somehow also some of the config files got demaged.
Rebooted the thing from backup and setup all the config files new and now it works like a charme :-)
quwax

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved