我在 ubuntu 10.4 上使用 apache2 + tomcat。我在 Tomcat 服务器上运行,该服务器的 webapps 在 8080 上侦听
“http://internal:8080/dir/name-privatewebapp/”
“http://internal:8080/dir/name-publicwebapp/”
面向外部的 Apache 服务器正在代理域的请求。
我想将 "subdomain" 重新映射到 "domain.com/subdomain" ,因此任何其他请求都将被代理到适当的路径 / webapp
web.john-doe.domain.com ---> www.domain.com/web/john-doe
http://www.john-doe.com ---> www.domain.com/web/john-doe
http://www.foo-bar.com ---> www.domain.com/web/foo-bar
我无法单独使用 mod_rewrite 。我一直在研究 mod_proxy、mod_proxy_ajp 和 mod_rewrite
我希望服务器
- 从域中获取子域
- 确保子域不是 www、w 或 ww
- 在重写之前检查目录是否真的存在于“www.domain.com”上
- 如果目录不存在,它将保持为野域
- 最后,如果目录存在实际重写
我检查了几个链接并研究了http://www.askapache.com/htaccess/crazy-advanced-mod_rewrite-tutorial.html上的教程
这是没有成功的。
我遵循了这个 illumin-it.servehttp.com/wordpress/2012/01/redirecting-to-as-using-apache/ 并取得了一些成功。
但是,我希望服务器通过重写和代理即时执行此操作,而无需编辑每个子域的虚拟主机。
这是我更新的虚拟主机文件
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName www.domain.com
ServerAlias www.domain.com domain.com *.domain.com
RedirectPermanent / http://www.domain.com
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
# Prevent looping this rule
# RewriteCond %{ENV:REDIRECT_STATUS} !200
# RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
# RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9.-]+).domain.com$ [NC]
# RewriteRule (.*) /%2/$1 [L]
# change the "." in the path to "/"
# RewriteRule ^(.*)\.(.*)/(.*)$ /$1/$2/$3 [L]
#redirect domain.com to www.domain.com
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteRule ^ http://www.domain.com%{REQUEST_URI} [R=301,NC]
</IfModule>
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
# ErrorLog /var/log/apache2/webapp_error.log
# LogLevel warn
# CustomLog /var/log/apache2/webapp_access.log combined
ProxyPass / ajp://localhost:8009/
ProxyPassReverse / ajp://localhost:8009/
# ProxyPass /user/ http://localhost:8080/user/privatewebapp/
# ProxyPassReverse /user/ http://localhost:8080/user/privatewebapp/
#
# ProxyPass /publicweb/ http://localhost:8080/dir/publicwebapp/
# ProxyPassReverse /publicweb/ http://localhost:8080/dir/publicwebapp/
#
# ProxyPass / http://localhost:8080/dir/publicwebapp/
# ProxyPassReverse / http://localhost:8080/dir/publicwebapp/
ServerName www.domain2.com
ProxyRequests Off
ProxyPreserveHost On
RewriteEngine On
RewriteCond %{HTTP_HOST} ^dir/publicwebapp$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [R=301,L]
<Location / >
Order deny,allow
Allow from all
RedirectMatch ^/$ /dir/publicwebapp/
ProxyPass ajp://localhost:8009/
ProxyPassReverse ajp://localhost:8009/
</Location>
</VirtualHost>
我也试过... http://httpd.apache.org/docs/2.0/rewrite/rewrite_guide.html
假设 www.username.host.com/anypath 内部映射到 www.host.com/home/username/anypath
作为使用 Java 服务器创建的站点应用程序,我认为仅使用 mod_rewrite 是行不通的。
虚拟用户主机
RewriteCond %{HTTP_HOST} ^www\.[^.]+\.host\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^www\.([^.]+)\.host\.com(.*) /home/$1$2