简单的apache重定向

时间:2015-03-02 20:43:28

标签: apache mod-rewrite

如何将所有图片从www.example.com和example.com重定向到images.example.com?

以下是我在网上找到的一些代码但我不太了解它并没有按预期工作

修改

#step 1 redirect ALL images from any domain to images.example.com
RewriteCond %{REQUEST_URI} ^/(.+)\.(gif|png|jpg|jpeg|jfif|bmp|css|js)$ [NC]
RewriteRule ^(.*)$ http://images.example.com/$1 [L,R=301]

#step 2 redirect non www to www
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

#step 3 redirect ALL non images from images.example.com to www.example.com
RewriteCond %{REQUEST_FILENAME} !\.(gif|png|jpg|jpeg|jfif|bmp|css|js)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

任何帮助都会非常感激!

谢谢!

1 个答案:

答案 0 :(得分:0)

如果要将所有图像重定向到子域,请执行此操作。

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(.+)\.(gif|png|jpg|jpeg|jfif|bmp|css|js)$ [NC]
RewriteRule ^(.*)$ http://images.example.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^images\.example\.com [NC]
RewriteCond %{REQUEST_URI} !^/(.+)\.(gif|png|jpg|jpeg|jfif|bmp|css|js)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]