Welcome to WebmasterWorld Guest from 50.19.190.144

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirect image path

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

Junior Member

joined:July 28, 2011
posts: 81
votes: 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
12:33 pm on June 19, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10542
votes: 8


^/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.
12:48 pm on June 19, 2013 (gmt 0)

Junior Member

joined:July 28, 2011
posts: 81
votes: 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]
12:55 pm on June 19, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10542
votes: 8


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.
1:47 pm on June 19, 2013 (gmt 0)

Junior Member

joined:July 28, 2011
posts: 81
votes: 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]
6:28 pm on June 19, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

joined:Apr 9, 2011
posts:12693
votes: 244


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.
6:38 pm on June 19, 2013 (gmt 0)

Junior Member

joined:July 28, 2011
posts: 81
votes: 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.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members