Httpd, Tomcat, mod_rewrite Subdomain Issue

So I have been struggling with an issue with Apache web server and Apache Tomcat app server set up in a proxy setup. Here is the scenario:

We have our Java web application and when we deploy it and go to our web site it works great. I will change the names for sake of security. So if I go to the web server will forward the request to Tomcat and the application will load fine.

Now we have some folders under ourapp that direct us to different locations. For example, if I go to it loads one page and if I go to it goes to another. I think you get the point.

Now I want to use subdomains to direct users to these folders so the URLs are pretty. So I set up an .htaccess file to rewrite the URLs. This is what the .htaccess file looks like:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\\.example\\.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\\.example\\.com [NC]
RewriteRule (.*) /ourapp/%1/index.html [NC,QSA,L]

Now when I go to [FONT=“Courier New”][/FONT] A page loads (because I get no 404) but none of the resources inside the HTML page loads. All of the resources (images, CSS files, JavaScript files, etc) are defined as relative links to /ourapp/folderA or /ourapp/folderB respectively.

I think this has something to do with our Java app having a path of /ourapp because when I manually type in [FONT=“Courier New”][/FONT] the file will load.

What I want to do is when someone goes to the subdomain that it rewrites so the relative paths will still work. I need the rewrite to take into account the application folder (ourapp/). I hope this all makes sense. Thank you in advance for your assistance.


This forum is about learning so don’t worry about not understanding. I’m here to help you (and other members) learn. When I get pedantic with one, it’s not meant as an insult but to teach others who may not wish to ask the necessary question: Why?

Okay, my first thought was that your subdomains have no business working UNLESS they’ve been setup as subdomains (if you’re using cPanel, it’s the subdomains button - but that creates a DocumentRoot pointing to the subdirectory with the name of the subdomain by default). However, it seemed that your subdomains are working, ergo the question.

Of course, if the server (i.e., httpd.conf) is yours, then you could have Dynamically configured mass virtual hosting (wildcard subdomains) setup.

Without seeing your .htaccess, I have to ask why you’re not using relative links (assuming a common DocumentRoot) or absolute links (assuming different DocumentRoots for each (sub)domain). Care to comment?



I do have complete control over the server. The app is not on any sort of shared hosting. We do have wildcard subdomains set up because we do have one other subdomain that is already functioning as intended. Inside all of our HTML pages we have relative links. The links are either relative to the current page or relative to our Java app’s context root. I will explain using my earlier example.

If our domain is and I am visiting, all the paths should be relative to /ourapp/ (context root). When I move it over to a subdomain like, I still want my relative links to for. For exmaple, I have a relative link in the index.html file that looks like this: …/images/testimage.png. This goes up one folder from folderA/ into the ourapp/ and then back down to the images/ folder. I want that to still work in the mod_rewrite subdomain setup.

I guess what I am saying is I want my document root to include /ourapp/. But our app is a Java app that is running on Tomcat. Apache is just the front for our application. It is proxying all requests to Tomcat (via proxy_ajp) sitting behind Apache so the web server has limited understanding of the application.

I hope this all makes sense.

Actually, I asked about apples and you responded with oranges.

If you have a subdomain that is functioning properly, it’s registered with your DNS server as well as your local VirtualHost with a physical address for the DocumentRoot (each domain/subdomain is treated like a separate domain). Are your subdomains using the domain’s DocumentRoot as their own or are they using their subdirectory as their DocumentRoot (if the later, then{anything but folderA} is outside folderA’s DocumentRoot/file structure and cannot be accessed with relative links.




I’m a bit confused as to where your subdomains are pointing. If they are pointing at their subdirectory on the main domain, they do NOT have (relative link) access to the files outside their own directory (it’s a “webspace” issue for that [sub]domain). Please clarify.



Let me give you an example that will hopefully clarify what I am talking about. Lets say we have a stylesheet called screen.css. In our original setup the file would be at a URL of What I would like is to have the URL be Inside the HTML file, the file is linked using a relative URL like this: css/screen.css where this is relative to I hope this helps. Thanks again.

The document root in apache hasn’t changed from the default. It is still domain root (/ or This is for all domains and subdomains as far as I can tell because we are only using apache for proxying requests over to Tomcat.

No worries if you don’t know Tomcat. I appreciate all your help regardless if a solution is provided. Thanks.


Well, I still don’t know where the DocumentRoots are located, main domain and subdomains. That is a problem for me (and, perhaps, Apache). However, a bigger problem (for me) is the Tomcat as I have no knowledge of its operation nor of how it “plays with” Apache.



Sorry about that. I guess I didn’t understand your question. I am sort of new at this server stuff. :frowning:

I am pretty sure we have the subdomains still pointing to the domain’s document root (which I don’t think includes /ourapp/). We are trying to do this subdomain stuff as dynamically as we can so when we add a folder underneath /ourapp/ (like /ourapp/folderA) we automatically get the subdomain without having to go into Apache’s configuration each time. That is why we are trying to use mod_rewrite.