Cảnh Kiều Minh
Cảnh Kiều Minh

Reputation: 1

.htaccess rewrite subdomain to domain for all image

My domain: example.com

SubDomain: admin.example.com

I want to rewrite all image url like:

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

This is my .htaccess but it does not work:

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

Upvotes: 1

Views: 359

Answers (2)

Amit Verma
Amit Verma

Reputation: 41219

This should work :

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

Upvotes: 1

Drazisil
Drazisil

Reputation: 3343

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

Confirmed at http://htaccess.madewithlove.be/

Upvotes: 0

Related Questions