【发布时间】:2013-12-19 09:29:19
【问题描述】:
我有一个域,其中除 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]
仅供参考。真的要感谢 jon 的答案,我只是根据自己的需要对其进行了调整。
【问题讨论】:
标签: image .htaccess redirect subdomain