Welcome to WebmasterWorld Guest from 34.229.126.29

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Does the mod rewrite work for image redirects ?

mod rewrite

     
8:35 pm on Jul 24, 2016 (gmt 0)

New User

joined:July 24, 2016
posts:7
votes: 0


Hello,

I want to redirect old image names to the new location.

This is what I have ... but I must have made a mistake

# CORRECTING IMAGE LOCATION
# THIS : http://example.com/images/sample-images-name.jpg
# MUST REDIRECT TO : http://example.com/photos/differentsampleimagename.jpg

RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^images/(.*?)-a-[.*?]$ photos/$1.jpg [NC,QSA,L]

[edited by: engine at 8:49 am (utc) on Jul 25, 2016]
[edit reason] please use example.com [/edit]

10:26 am on July 25, 2016 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 328
votes: 24


mod_rewrite works with URLs, regardless of whether that maps to an image, PDF, HTML document, etc.

You might need to pick a better example... your example image filenames don't match at all with your example code? You can't map "sample-images-name.jpg" to "differentsampleimagename.jpg" with a "wildcard" redirect, so I suspect your real image filenames match a different pattern?

What are the URLs you need to map from/to?

Your current code suggests a "from" filename something like: "thing-a-majig.jpg", which maps to "thing.jpg"?! Although the RewriteRule pattern [.*?] is probably not what you intended?
6:08 pm on July 25, 2016 (gmt 0)

New User

joined:July 24, 2016
posts:7
votes: 0


Hi,

Actually my post was edited "by engine" and the paths were changed.
So here are the original paths but using "example.com" as requested ...

# CORRECTING IMAGE LOCATION
# THIS : http://example.com/images/sample-images-name.jpg
# MUST REDIRECT TO : http://example.com/photos/differentsampleimagename.jpg

RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^images/(.*?)-a-[.*?]$ photos/$1.jpg [NC,QSA,L]


Thanks for any help...

David

.

[edited by: not2easy at 8:16 pm (utc) on Sep 1, 2016]
[edit reason] Re-edited specifics [/edit]

11:38 pm on July 25, 2016 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 328
votes: 24


Ah ok, that makes more sense. Try something like the following RewriteRule:


RewriteRule ^images/(.+?)-a- photos/$1.jpg [NC,L]


That should match something before the "-a-" (which is captured for the $1 backreference in the substitution). The rest of the filename is simply ignored. The QSA flag would not seem to be necessary here. The query string will be passed through by default anyway (but do you even have a query string on your images?). I'd also question whether you need the NC (NOCASE) flag - do you need a case-insensitive match? Otherwise the NC flag should be omitted on the RewriteRule.

Also, do you need the preceding RewriteCond directive - do you have multiple hostnames?
11:01 am on July 26, 2016 (gmt 0)

New User

joined:July 24, 2016
posts:7
votes: 0


Hi Whitespace,
Thanks for the help.

Some Answers ...
but do you even have a query string on your images?
No - I don't have any query, just a straight url.

do you need a case-insensitive match?
No, probably not !

do you have multiple hostnames?
Yes, many.

I have one question ...
Shouldn't there be an end character in there ... the $ sign ?
Like this ?

RewriteRule ^images/(.+?)-a- $ photos/$1.jpg [L]


Thanks again ...

.
3:47 pm on July 26, 2016 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 328
votes: 24



Shouldn't there be an end character in there ... the $ sign ?


An end anchor ($) would not seem to be necessary for what you are doing. (But you also should not have a space before it - although maybe that was for illustrative purposes?). You only seem to be interested in the start of the filename. From your earlier example/pattern, the filename can end in anything and is discarded - so there is no need to match it? For example, a regex such as ^foo.*$ is the same as the simpler ^foo - both mean the same thing: "starts with foo".

If the "-a-" can only ever occur at most once in the filename then you could simplify the pattern further and remove the "?", for example:


RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^images/(.+)-a- photos/$1.jpg [L]


The "?" in the pattern ".+?" makes it non-greedy.
2:10 am on July 27, 2016 (gmt 0)

Senior Member from US 

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

joined:Apr 9, 2011
posts:15636
votes: 795


RewriteRule ^images/(.+?)-a-

I'd feel much more comfortable if that could be expressed as simply
^images/(\w+)-a-

and then the server won't have to ricochet back and forth while waiting to see if there's more than one -a- in the string. Unless, that is, there really might be a non-word character--in which case the pattern needs to be more tightly constrained.

A non-final .+ or .* should always be treated as a last resort.
9:26 pm on Aug 8, 2016 (gmt 0)

New User

joined:July 24, 2016
posts:7
votes: 0


HI Thanks for your answers,

I have tried your suggestions but for some reason it is not
redirecting to the photo.

This is my simple .htaccess file:


# REDIRECTING IMAGES
# http://example.com/images/buseng1-a-speaking-english-dictionary-online-business-english-report-writing-exercises.jpg
# http://example.com/photos/buseng1.jpg

RewriteCond %{HTTP_HOST} ^.example.com$ [NC]
RewriteRule ^images/(.*)-a- photos/$1.jpg [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ 404.php?url=$1 [L]

When I enter the url, I just get the 404 page ?

What can be the problem ?

( If you swap the word combld for example you will get the real address to see
the live error. )

Thanks.
10:00 pm on Aug 8, 2016 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 328
votes: 24



RewriteCond %{HTTP_HOST} ^.example.com$ [NC]


You have an extra "." (dot) at the start of the pattern (before the domain name), so this is probably failing to match. (Dots should be backslash escaped in the regex to match a literal dot, and not any character - although I neglected to do this in my post above - copy/paste faux pas!). So, providing you are already canonicalising to the apex domain (ie. no "www" subdomain), then this should be:


RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members