Apache 将子域重定向到不带www的IP

Apache 将子域重定向到不带www的IP,apache,.htaccess,redirect,subdomain,Apache,.htaccess,Redirect,Subdomain,我试了几个小时,差点发疯。我想将sub.example.com重定向到远程服务器文件上的文件 所以我想将我的子域(有www/没有www)重定向到x.x.x.x/example/photo.jpg 对于www.sub.example.com,一切正常。它完全重定向到x.x.x.x/example。 说到sub.example.com,我有404 RewriteEngine on RewriteCond %{HTTP_HOST} ^sub\.example\.com$ RewriteCond %{

我试了几个小时,差点发疯。我想将sub.example.com重定向到远程服务器文件上的文件

所以我想将我的子域(有www/没有www)重定向到x.x.x.x/example/photo.jpg

对于www.sub.example.com,一切正常。它完全重定向到x.x.x.x/example。 说到sub.example.com,我有404

RewriteEngine on

RewriteCond %{HTTP_HOST} ^sub\.example\.com$
RewriteCond %{HTTP_HOST} ^www\.sub\.example\.com$
RewriteRule ^(.*)$ "http\:\/\/x\.x\.x\.x\/example/$1" [R=301,L]

RewriteCond被隐式地“和”合并在一起,这意味着您永远不会得到同时是“sub.example.com”和“www.sub.example.com”的%{HTTP_HOST},因此RewriteRule永远不会被使用。您可以在第一个后添加
[或]

RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.sub\.example\.com$
这样就可以满足RewriteCond并使用该规则,或者只使用以下命令:

RewriteCond %{HTTP_HOST} ^(www\.)?sub\.example\.com$
也就是说“www.”在比赛中是可选的。

在上使用此代码重写引擎 RewriteCond%{HTTP_HOST}^example.com[NC] 重写规则^(.*)$$1[R=301,L]