htaccess css / js / images基于子域字符串的重定向

时间:2015-09-16 19:47:28

标签: .htaccess url-redirection

我正在尝试根据子域名字符串重定向图像,css,js和其他一些文件夹:

WWW:

  • www .domain.com / css / main.css - > / 网站 /css/main.css
  • www .domain.com / images / logo.png - > / 网站 /images/logo.png

所有其他子域名:

  • other1 .domain.com / css / main.css - > /的应用 /css/main.css
  • other2 .domain.com / css / main.css - > /的应用 /css/main.css
  • otherX .domain.com / js / script.js - > /的应用 /js/script.js

所有其他请求必须通过index.php

进行路由

到目前为止我所做的部分工作是:

#THIS BLOCK WORKS
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L] # <--DON'T KNOW ABOUT THESE FLAGS

# SUBDOMAIN - THIS PART WORKS
RewriteCond %{HTTP_HOST} !^www\.subdomain\.[a-z] <--# ADDED [a-z] .dev or .com
RewriteRule ^css/(.*)$ /app/css/$1 [R,NC,L]
RewriteRule ^js/(.*)$ /app/js/$1 [R,NC,L]
RewriteRule ^assets/(.*)$ /app/assets/$1 [R,NC,L]
RewriteRule ^images/(.*)$ /app/images/$1 [R,NC,L]
RewriteRule ^uploads/(.*)$ /app/uploads/$1 [R,NC,L]

# OR

# WWW - THIS PART WORKS
RewriteCond %{HTTP_HOST} ^www\.subdomain\.[a-z] <--# ADDED [a-z] .dev or .com
RewriteRule ^css/(.*)$ /site/css/$1 [R,NC,L]
RewriteRule ^js/(.*)$ /site/js/$1 [R,NC,L]
RewriteRule ^assets/(.*)$ /site/assets/$1 [R,NC,L]
RewriteRule ^images/(.*)$ /site/images/$1 [R,NC,L]
RewriteRule ^uploads/(.*)$ /site/uploads/$1 [R,NC,L]

# BUT NOT TOGETHER

RewriteRule ^.*$ index.php [NC,L]

我不能让这两套规则一起工作。

我希望有类似的东西:

RewriteCond%{HTTP_HOST}!^ www.domain。[a-z]

RewriteRule ^(。?)。(php | css | js | jpg | jpeg | png | pdf)/(。)$ / app / css / $ 1 [L]

提前谢谢!

1 个答案:

答案 0 :(得分:0)

只有一个不眠之夜,我找到了它!

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^.*$ - [NC,L]

RewriteCond %{HTTP_HOST} !^www\.domain\.[a-z]
RewriteRule ^(css|js|images|assets)/(.*)$ /app/$1/$2 [R,NC,L]

RewriteCond %{HTTP_HOST} ^www\.domain\.[a-z]
RewriteRule ^(css|js|images|assets)/(.*)$ /site/$1/$2 [R,NC,L]

RewriteRule ^.*$ index.php [NC,L]