htaccess转发一些子域名

时间:2014-09-16 23:43:09

标签: apache .htaccess mod-rewrite

我正在转发这样的子域名(将sub.example.com转换为example.com/subdomain/sub)

# the rewrite that forwards sub-domains as guide 
RewriteCond %{HTTP_HOST} ^(.+?)\.(.*) [NC]
RewriteRule (.*) http://%{SERVER_NAME}:9081/subdomain/%1/$1 [P,QSA,NE]

哪个效果很好,但会打破CSS / JS文件的所有相关链接。

我希望我可以提出另一条规则,无论子域名如何,都会停止转发任何example.com/resources请求,即

RewriteRule ^resources\/$ http://%{SERVER_NAME}:9081/resources/$1 [QSA,P,L]

目前转发到example.com/subdomain/resources,当我希望它转发到example.com/resources时。

我知道如何在特定子文件夹上覆盖子域的重写规则,或者这是不可能的?其他解决方案是CSS / JS中的绝对URL,我希望避免使用它。

由于

1 个答案:

答案 0 :(得分:1)

您可以尝试添加条件:

# the rewrite that forwards sub-domains as guide 
RewriteCond %{REQUEST_URI} !\.(js|css)$ [NC]
RewriteCond %{HTTP_HOST} ^(.+?)\.(.*) [NC]
RewriteRule (.*) http://%{SERVER_NAME}:9081/subdomain/%1/$1 [P,QSA,NE]
相关问题