Forum Moderators: phranque

Message Too Old, No Replies

sub.domain with dots and mod rewrite

apache+mod_rewrite+mod_jk -> tomcat

         

hendrakieran

10:15 am on Sep 24, 2007 (gmt 0)

10+ Year Member



Hi All,

I'm new to the forum and desperately need help after searching the forum and not able to find solution to my problem.

What I'm trying to achive:
a. subdomain.example.com to be sent to a jsp page in Tomcat with subdomain as parameter
b. sub.domain.example.com to be sent to a jsp page in Tomcat with sub.domain as paramater (NOTICE the dot sub.domain)
b. sub.dom.ain.example.com to be sent to a jsp page in Tomcat with sub.dom.ain as paramater (NOTICE the dot sub.domain)

In otherwords, only example.com is fixed, whatever before that and doesn't matter how many dot involved, need to be considered as a single value/parameter when passing to the JSP

My problem:
- The RewriteCond (includede below) that I have only handles case (a) above (no dot at all in the subdomain)

This is what I have:
- Apache 2.2.4 + mod_jk + mod_rewrite + mod_vhost_alias
- Tomcat 5.5.17
- a working wildcard dns *.example.com

The VirtualHost in my httpd.conf currently looks like this:
[codes]
<VirtualHost *:80>
ServerName example.com
ServerAlias *.example.com
JkMount /foo/*.jsp ajp13w
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^example\.com$
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z][a-z0-9_\-]{2,11})\.example\.com
RewriteRule ^/(.*)$ /foo/hello.jsp?name=%2 [PT]
</VirtualHost>
[/codes]

Can anyone help to suggest a better RewriteCond?
Thanks in advance!

Rgds,
Hendra

jdMorgan

10:42 pm on Sep 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your first two RewriteConds are redundant, since the third RewriteCond certainly would not match if either of the first two would not match match. So they can be deleted.

The only remaining problem is that the pattern in your third RewriteCond is too specific -- It will not match any strings with periods in them. So a nice, simple solution might be:


RewriteCond %{HTTP_HOST} ^(www\.)?(.+)\.example\.com
RewriteRule ^/(.*)$ /foo/hello.jsp?name=%2 [PT,L]

Do you need to retain the "requested page" to pass to Tomcat? I see no provision to do that. You could pass it as a query parameter if the back-end supports that, by adding, for example:

RewriteRule ^/(.*)$ /foo/hello.jsp?name=%2[b]&page=$1[/b] [PT,L]

Jim

hendrakieran

1:19 am on Sep 25, 2007 (gmt 0)

10+ Year Member



Thanks Jim, it works like magic! I've also followed your other suggestions and make the necessary changes.

Thank you so very much ;)

hendrakieran

7:15 am on Sep 25, 2007 (gmt 0)

10+ Year Member



btw, anyone here familiar with lighttpd? can I achieve the above by replacing Apache with it? Does it support all the Apache modules that exist today?

Hendra