Apache virtual host and dynamic domains

I have a java application that responds to several domains and uses a specific apache virtual host for each domain. This is because Apache is faster than tomcat to serve static resources.

You must do this at runtime without configuring the apache configuration. To perform this action, I use the VirtualDocumentRoot directive, as described below:

AddType text/html .html .shtml AddOutputFilter INCLUDES .html .shtml NameVirtualHost *:80 UseCanonicalName Off <VirtualHost *:80> ServerName domain.com ServerAlias * # Define virtual host directory, using entire domain VirtualDocumentRoot /path/to/whosts/%0 # Define directory access <Directory "/path/to/whosts/"> Options -Indexes MultiViews +Includes Order allow,deny Allow from all </Directory> # Define Java Proxies <Proxy *> AddDefaultCharset Off Order deny,allow Allow from all </Proxy> # Allow Libs (static resources) to access apache directly ProxyPass /libs ! ProxyPass / ajp://localhost:8009/ ProxyPassReverse / ajp://localhost:8009/ </VirtualHost> 

This does not work, because if I try to access www.domain.com, it is different from access to domain.com.

Do you think it’s a good idea to register a symbolic link from www.domain.comto domain.com ???

Is there another way to do this? I am very poor at managing Apache ...

Thanks a lot!

Qiao, David.

+4
source share
2 answers

I regularly use symbolic links to bind multiple domains to the same webrot when performing such configurations, there is no obvious harm in this solution, so there is definitely no reason to avoid it.

+2
source

A good way would be to determine which type of URL should be canonical, and use mod_rewrite to redirect URLs to it - for example, match requests to domain.com and redirect them to www.domain.com . There are many tutorials on how to do this online that you can easily find.

On top of my head, you can use something like:

 RewriteEngine on RewriteCond %{HTTP_HOST} !^www\.$ [NC] RewriteRule ^(.*) http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

This will cause problems if you use SSL, although due to hardcoded http:// . I think you could change the RewriteRule line to the following to avoid this:

 RewriteRule ^(.*) %{SERVER_PROTOCOL}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 
0
source

All Articles