Changing STATIC URLs with mod_rewrite

Discuss practical ways rearrange URLs using mod_rewrite.

Changing STATIC URLs with mod_rewrite

Postby eporue » Sun Feb 27, 2005 3:05 am

Hello,

I have a few thousand pictures with the format 5566.jpg, 3212q.jpg, etc. in a directory called /pics.

I want to change the URLs pointing at them them to playlingerie.com/pics/lingerie_5566.jpg, playlingerie.com/pics/lingerie_3212q.jpg, etc

The reason for doing it is improving my positioning in Google's image search.

Is this possible ?? Anybody knows how to write the mod_rewrite ?? I have tried several things but nothing works ....

Thank you
eporue
 
Posts: 4
Joined: Sun Feb 27, 2005 3:02 am

Postby Caterham » Sun Feb 27, 2005 5:40 am

playlingerie.com/pics/lingerie_5566.jpg,
use y hyphen instead
playlingerie.com/pics/lingeri-_5566.jpg
, a underscore is not a word separator

this will rewrite requests of playlingerie.com/pics/lingerie-5566.jpg internally to playlingerie.com/pics/5566.jpg
.htaccss in your doc root wrote:
Code: Select all
RewriteEngine On
RewriteRule ^.+-([0-9]+\.jpg)$ /pics/$1 [L]
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm

Postby eporue » Mon Feb 28, 2005 3:07 pm

I cannot make it work.

Actually, I put:
Code: Select all
RewriteEngine On
RewriteRule ^.+-([0-9]+\.jpg)$ /pics/lingerie-$1
RewriteRule ^.+-([0-9]+\.jpg)$ /pics2/lingerie-$1 [L]


(there are pics in several directories), but is not working.

Any suggestion ?? :confused:
eporue
 
Posts: 4
Joined: Sun Feb 27, 2005 3:02 am

Postby Caterham » Tue Mar 01, 2005 5:10 am

this will not work, because the pattern is the same.
use this instead
Code: Select all
RewriteRule ^(.+)/[^/]+-([0-9]+\.jpg)$ /$1/$2 [L]


and call ie. /img1/dir2/test-23.jpg for /img1/dir2/23.jpg
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm

Postby Guest » Wed Mar 02, 2005 2:02 am

And where would the "test" part go ? Would it be like this ?



Code: Select all
RewriteRule ^(.+)/[^/]test-+-([0-9]+\.jpg)$ /$1/$2 [L]


By the way, thank you ery much for the help.
Guest
 

Postby Caterham » Wed Mar 02, 2005 8:38 am

no, you're using the RegEx/rule above and call through your browser/<img src="...
http://example.com/img1/dir2/any-keyword-here-23.jpg or http://example.com/img1/dir2/an-other-test-23.jpg or
http://example.com/img1/dir2/test-23.jpg etc.

any the rule will call internal /img1/dir2/23.jpg
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm

Postby Guest » Sat Mar 12, 2005 6:37 am

It works beautiful. I even changed it for htm files.

But I still have one problem: when the jpg file contains a letter (for instace, instead of 33.jpg, it is called l33.jpg or xx33e.jpg), I get an error.

For instance , in http://lenceria.com/sexy-lingerie-lenceria-modelo-621.htm it cannot find the jpg.

Thank you again for your help.
Guest
 

Postby Caterham » Sat Mar 12, 2005 12:04 pm

add some chars in the pattern:
RewriteRule ^(.+)/[^/]+-([a-z0-9]+\.jpg)$ /$1/$2 [L]
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm

Postby eporue » Sun Mar 13, 2005 5:45 am

This solves the file names like x3444.jpg, but not the ones like 3444q.jpg or x3443q.

I tried to change it to:

Code: Select all
RewriteRule ^(.+)/[^/]+-([a-z0-9]+\.jpg)$ /$1/$2 [L]
RewriteRule ^(.+)/[^/]+-([0-9a-z]+\.jpg)$ /$1/$2 [L]


But didn't work, and to :
Code: Select all
RewriteRule ^(.+)/[^/]+-([a-z0-9a-z]+\.jpg)$ /$1/$2 [L]

but didn't either.
eporue
 
Posts: 4
Joined: Sun Feb 27, 2005 3:02 am

Postby Caterham » Sun Mar 13, 2005 8:43 am

Code: Select all
[a-z0-9]+
will work for a66a, a666, 777dd, ds32f24wf.
[0-9a-z]+
is the same. Everything inside a [] is a character group, the order doesn't matter. The RegEx is correct, tested with Apache 1.3x and 2.0.5x.

This does not work for capital letters. If you've capital letters, you've to use

Code: Select all
RewriteRule ^(.+)/[^/]+-([a-zA-Z0-9]+\.jpg)$ /$1/$2 [L]
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm

Next

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 21 guests

cron