l0ckm4
l0ckm4

Reputation: 747

.htaccess rewrite rule - change url of any requested jpg to go to another server

I am trying to setup a rewrite rule in .htaccess for a specific folder and its subfolders so that any request for a jpg image file gets redirected to another domain. The problem is the path is also getting redirected which I do not want to happen.

Example - I need

http://www.example.com/test/media/0/1/test.jpg to get directed to http://www.example1.com/img/gallery/test.jpg

and

http://www.example.com/test/media/5/7/test1.jpg to get directed to http://www.example1.com/img/gallery/test1.jpg

I have created a .htaccess file in ../test/media folder and in there I have at the moment

RewriteEngine on
RewriteCond %{HTTPS} !on
RewriteRule ([^.]+\.(jpg))$ http://www.example1.com/img/gallery/$1

the request is being redirected but it gets redirected to

http://www.example1.com/img/gallery/0/1/test.jpg

when I need it to be

http://www.example1.com/img/gallery/test.jpg

Could anyone help please?

Upvotes: 0

Views: 459

Answers (1)

umka
umka

Reputation: 1655

Modify yor rule this way:

RewriteRule /([^./]+\.(jpg))$ http://www.example1.com/img/gallery/$1

And it will take only filename after last slash.

Upvotes: 1

Related Questions