我在ubuntu 10.4上使用apache2 + tomcat。我正在tomcat服务器上运行,该服务器上有8080监听的webapps
“HTTP://内部:8080 /目录/名称-privatewebapp /”
的 “http://内部:8080 /目录/名称-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
我想要服务器
我检查了几个链接,并在http://www.askapache.com/htaccess/crazy-advanced-mod_rewrite-tutorial.html
上学习了教程没有成功。
我跟着这个 illumin-it.servehttp.com/wordpress/2012/01/redirecting-to-as-using-apache/ 有点成功。
但是,我希望服务器能够通过重写和代理动态执行此操作,而无需为每个子域编辑vhost。
这是我更新的vhost文件
<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
答案 0 :(得分:0)
在类似的情况下,我通常把这样的东西放到webroot中的.htaccess(/var/www/.htaccess):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} subdomain1.domain.com
RewriteCond %{REQUEST_URI} !^/subdomain1
RewriteRule ^(.*)$ one/$1 [L]
RewriteCond %{HTTP_HOST} subdomain2.domain.com
RewriteCond %{REQUEST_URI} !^/subdomain2
RewriteRule ^(.*)$ two/$1 [L]
RewriteRule ^$ / [L]
RewriteRule (.*) /$1 [L]
</IfModule>
所以我有
subdomain1.servername.com -> /var/www/subdomain1
subdomain2.servername.com -> /var/www/subdomain2
servername.com -> /var/www
也许这会对你有帮助吗?
答案 1 :(得分:0)
你的第一套规则:
#redirect domain.com to www.domain.com
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule .+ http://www.domain.com%{REQUEST_URI} [R=301,NC]
如果您没有空请求,则取代您拥有的任何子域。第二套规则接近你想要的。但是它们循环并且它们不会将多个子域条目转换为目录路径,因此请尝试将这组规则首先:
# 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]
然后将您的“非www到www”规则更改为此(并按照上述规则):
#redirect domain.com to www.domain.com
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteRule ^ http://www.domain.com%{REQUEST_URI} [R=301,NC]