.htaccess将子域重写为所有图像的域

时间:2015-08-29 05:32:25

标签: image .htaccess mod-rewrite

我的域名:example.com

SubDomain:admin.example.com

我想重写所有图片网址:

admin.example.com/img/image1.jpg -> example.com/img/image1.jpg
admin.example.com/img/image2.png -> example.com/img/image2.png
admin.example.com/img/image3.gif -> example.com/img/image3.gif

这是我的.htaccess,但不起作用:

RewriteCond %{HTTP_HOST} ^admin.example.com$
RewriteCond %{REQUEST_URI} \.(png|jpg|gif|jpeg|ico|bmp)$
RewriteRule (.*) http://example.com/%{REQUEST_URI} [R=301,L]

2 个答案:

答案 0 :(得分:1)

这应该有效:

RewriteCond %{HTTP_HOST} ^admin.example.com$ [NC]
RewriteCond %{THE_REQUEST} /img/([^\s]+) [NC]
RewriteRule ^ http://example.com/img/%1 [NC,R=301,L]

答案 1 :(得分:0)

RewriteCond %{HTTP_HOST}  ^admin.example.com
RewriteCond %{REQUEST_URI} \.(png|jpg|gif|jpeg|ico|bmp)$
RewriteRule (.*) http://example.com/%{REQUEST_URI} [R=301,L]

http://htaccess.madewithlove.be/

确认