问题
I have a java application responding multiple domains and uses, for each domain, a specific apache virtual host. This because Apache is faster than tomcat, to serve static resources.
The need is to do that at runtime, without restart apache configuration. To perform this action I'm using 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 well, because if I try to access to www.domain.com, is different than access to domain.com.
Do you think is a good idea to register a symbolic link from www.domain.comto domain.com???
Exists another way to do that? I'm really poor in apache managing...
Thank's a lot!
Ciao, Davide.
回答1:
I regularly use symlinks to link multiple domains to the same webroot when doing configurations similar to this, there is no explicit harm in that solution so definitely no reason to shy away from it.
回答2:
A good way would be to decide which type of URL should be the canonical one, and use mod_rewrite
to redirect URLs to it - for instance, match requests to domain.com
and redirect them to www.domain.com
. There's plenty of tutorials on how to do this available on the web which you should be able to find easily.
Off the top of my head, you could use something like:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.$ [NC]
RewriteRule ^(.*) http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
That would cause problems if you're using SSL, though, due to the hardcoded http://
. I think you could change the RewriteRule line to the following to avoid that:
RewriteRule ^(.*) %{SERVER_PROTOCOL}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
来源:https://stackoverflow.com/questions/6939739/apache-virtual-host-and-dynamic-domains