homepage Welcome to WebmasterWorld Guest from 54.161.190.9
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Redirect image path
sam222




msg:4585704
 12:22 pm on Jun 19, 2013 (gmt 0)

For image path redirect, I am trying to do below but no success. Can i have any help on it please:

URL is like:
example.com/dir1/dir2/dir3/dir4/imagefilename.jpg


RewriteRule ^/dir1/dir2/dir3/dir4/(.*)$ /images/test/dir3([^/]+\.(gif|png|jpg) [R=301,L]

where i want to redirect example.com/dir1/dir2/dir3/dir4/imagefilename.jpg to example.com/images/test/dir3/imagefilename.jpg

 

phranque




msg:4585708
 12:33 pm on Jun 19, 2013 (gmt 0)

^/dir1/dir2/dir3/dir4/(.*)$

if your RewriteRule is in the .htaccess file in the document root directory, the leading slash will be stripped and your Pattern string should reflect that.

/images/test/dir3([^/]+\.(gif|png|jpg)

since it's an external redirect, include the full protocol and hostname in the Substitution string.
also, since it's a substitution string and not a regular expression (pattern), you should have any capture groups or escape sequences.
you probably do want to use a backreference to reuse the captured file name.

sam222




msg:4585712
 12:48 pm on Jun 19, 2013 (gmt 0)

I have tried to do but no success. Could you please further help:

RewriteRule ^http://www.example.com/dir1/dir2/dir3/dir4/(.*)$ http://www.example.com/images/test/dir3([^/]+\.(gif|png|jpg) [R=301,L]

phranque




msg:4585714
 12:55 pm on Jun 19, 2013 (gmt 0)

the full protocol and hostname should be in the Substitution string, but not in the Pattern string.

the url you are testing with that Pattern will have the leading slash stripped.

you shouldn't have any regular expression-type junk in the Substitution string, but you should have the backreference to the captured image file name.

sam222




msg:4585727
 1:47 pm on Jun 19, 2013 (gmt 0)

RewriteRule ^dir1/dir2/dir3/dir4/(.*)$ http://www.example.com/images/test/dir3/$1 [R=301,L]

This works now fine but it doesn't display images now and goes in loop. Might be because of URL rewrite rule:

RewriteRule ^images/test/dir3/([^/]+\.(gif|png|jpg))$ /dir1/dir2/dir3/dir4/$1/$2 [L]

lucy24




msg:4585826
 6:28 pm on Jun 19, 2013 (gmt 0)

Wait a minute. What "URL rewrite rule"? If image URLs are already being rewritten, why bother to redirect them? Simply change the rewrite to point to the image's new home.

If your rules are really as posted, you've got a pure loop:

request a redirects to b, which in turn rewrites to a.

To stop the loop, the external redirect needs a preceding RewriteCond that says "only do this if %{THE_REQUEST} was for ^/dir1/dir2 et cetera". In other words, only redirect requests that haven't already been rewritten.

And, as with any redirect, fix the code that asks for the wrong URL. This is easier with images than with pages, because you only have to tweak things on your own site.

sam222




msg:4585832
 6:38 pm on Jun 19, 2013 (gmt 0)

If image URLs are already being rewritten, why bother to redirect them? Simply change the rewrite to point to the image's new home.


As images have been read by Google and if i don't do URL redirect for images i am afraid i will lose ranking in Google SERP for images. I might be wrong if you can correct me further please.

Could you please write me more details how can i make this condition for rewrite if you can write me an example please.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved