我有一个域,除了image \ css等以外的所有内容都由一个php文件处理。但是,在重组之后,很多图像已经从子域移动到主域。所以我正在寻找一种方法将所有image \ css文件重定向到主域,如果它们最初位于其中一个子域上的话。我目前的代码是
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.ico|\.bmp|\.css|\.ts|\.js)$
RewriteRule !^index\.php$ /index.php [L]
我尝试了几种方法来重定向它,但无论我尝试什么,我似乎都会破坏现有的规则。
由于
我提出的最终解决方案
RewriteEngine On
# Check the request isn't for the main domain
RewriteCond %{HTTP_HOST} !^domain\.com$
# Check the request is for a static resource
RewriteCond %{REQUEST_URI} \.(png|jpg|gif|jpeg|ico|bmp|css|ts|js)$
# Redirect to main domain
RewriteRule (.*) http://domain\.com/$1 [R=301,L]
# if the request isn't for index.php,
# (invisibly) redirect to index.php
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.ico|\.bmp|\.css|\.ts|\.js)$
RewriteRule !^index\.php$ /index.php [L]
仅供参考。实际上,我只是根据我的需要调整了答案。
答案 0 :(得分:1)
如果您的HTML中的网址仍然指向子域,则您需要在这些子域上设置htaccess重定向,而不是在主域上,因为请求仍将转到子域。
例如/var/www/vhosts/sub.domain.com/httpdocs/.htaccess
:
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(png|jpg|gif|jpeg|ico|bmp|css|ts|js)$
RewriteRule (.*) http://domain.com/$1 [R=301,L]
在/var/www/vhosts/sub2.domain.com/httpdocs/.htaccess
:
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(png|jpg|gif|jpeg|ico|bmp|css|ts|js)$
RewriteRule (.*) http://domain.com/$1 [R=301,L]
<强>更新强>
根据您的评论,Apache处理每个子域,就好像它是主要子域一样,试试这个:
RewriteEngine On
# Check the request isn't for the main domain
RewriteCond %{HTTP_HOST} !(www\.)?domain\.com$
# Check the request is for a static resource
RewriteCond %{REQUEST_URI} \.(png|jpg|gif|jpeg|ico|bmp|css|ts|js)$
# Redirect to main domain
RewriteRule (.*) http://domain.com/$1 [R=301,L]