Tomcat apache2无法将虚拟主机内的非www重定向到www

Tomcat apache2无法将虚拟主机内的非www重定向到www,tomcat,mod-rewrite,apache2,virtualhost,Tomcat,Mod Rewrite,Apache2,Virtualhost,我在tomcat(6)前面有一个apache(2.4.6)服务器。我希望所有到www.domain.com的请求永久重定向到domain.com。我目前的配置是: <VirtualHost *:80> ServerName www.domain.ma RewriteEngine on RewriteCond %{HTTP_HOST} ^domain\.ma RewriteRule ^(.*)$ http://www.domain.ma/$1 [R=permanent,L] Docu

我在tomcat(6)前面有一个apache(2.4.6)服务器。我希望所有到www.domain.com的请求永久重定向到domain.com。我目前的配置是:

<VirtualHost *:80>
ServerName www.domain.ma
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.ma
RewriteRule ^(.*)$ http://www.domain.ma/$1 [R=permanent,L]

DocumentRoot /var/www/domain.ma/htdocs

ProxyRequests Off
ProxyPreserveHost On

ProxyPassReverseCookiePath /portal /
ProxyPass /portal/index.htm http://127.0.0.1:8080/portal/index.htm

ProxyPass /images !
ExpiresByType image/* A2592000

</VirtualHost>

服务器名www.domain.ma
重新启动发动机
重写cond%{HTTP_HOST}^domain\.ma
重写规则^(.*)$http://www.domain.ma/$1[R=永久性,L]
DocumentRoot/var/www/domain.ma/htdocs
代理请求关闭
代理主机
ProxyPassReverseCookiePath/portal/
ProxyPass/portal/index.htmhttp://127.0.0.1:8080/portal/index.htm
ProxyPass/图片!
ExpiresByType图像/*A2592000
当我请求时,apache不会重定向到,有人能告诉我我的配置有什么问题吗?

试试这个:

ServerName domain.ma
ServerAlias www.domain.ma 

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

请注意,您的http_主机正则表达式并不完全正确。如果有人设法将
domain.masite.example.com
指向您的服务器,您仍然会重定向,因为您只匹配主机名的开头,而不是整个字符串。使用“ist”解决方案,我遇到了一个小问题:重定向到:双斜杠。我通过替换RewriteRule(.*)[R=301,L]中的RewriteRule(.*)[R=301,L]@user973296来纠正它,如果没有ServerAlias,Apache将不知道此虚拟主机适用于www.domain.ma和domain。ma@user973296对不起,我可能会用另一种方式来命名。i、 e.
ServerName www.domain.ma
ServerAlias domain.ma
as
www.
是主要的。-只是美学。