• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • Last Modified:

help with Apache Mod_Rewrite rule

Hello All,

I'm trying to rewrite a URL to make it more SEO friendly. I have tried many different rewrite rules but can't seem to get it working properly.

Here is what I'm trying to do. I would like to rewrite a URL like this:

http://photo.mysite.com/photos/_firstname_lastname_city_state_usernum_123.456.789C.jpg

The values of firstname, lastname, city, state, usernum amd photo number up until the C.jpg will be variable.

Here is another example:

http://photo.mysite.com/photos/_joe_smith_losangeles_ca_2345_123.456.789C.jpg

I would like to use Apache rewrite to strip out everything between /photos/ and 123.456.789C.jpg and replace the dots (periods) between 123,456 and 789 with / .

So I want the rewrite to look like this:

http://photo.mysite.com/photos/123/456/789C.jpg

Additional info: the last 9 digits before the "C.jpg" will always be 9 digits (ie. 123.456.789)

Any help will be greatly appreciated!!
0
chris0011
Asked:
chris0011
  • 2
  • 2
1 Solution
 
arober11Commented:
Hi

If you have access to the httpd.conf, stick something along the following lines in your VirtualHost definition, end check the mod_rewrite is loaded:

RewriteEngine On
RewriteRule /photos/_[a-zA-Z]*_[a-zA-Z]*_[a-zA-Z]*_[a-zA-Z][a-zA-Z]_[a-zA-Z0-9]*_([0-9]*).([0-9]*).([0-9]*C.jpg)   /photos/$1/$2/$3 [L]

Else you will need to loose the leading '/' and add the rules to a .htaccess file, e.g. for a .htaccess in your Document root:

RewriteEngine On
RewriteBase /
RewriteRule photos/_[a-zA-Z]*_[a-zA-Z]*_[a-zA-Z]*_[a-zA-Z][a-zA-Z]_[a-zA-Z0-9]*_([0-9]*).([0-9]*).([0-9]*C.jpg)   /photos/$1/$2/$3 [L]
0
 
chris0011Author Commented:
Hi Arober11,

Thanks for your help.

Unfortunately, the rewrite rule still isn't catching the URL.

When I go to -- http://10.10.1.42/photos/_keta_pat_ahemedabad_india_2345_000.001.994C.jpg I get this error in the apache error log:

[Wed Jan 13 12:20:26 2010] [error] [client 192.168.0.78] File does not exist: /mysite_photos_final/_keta_pat_ahemedabad_india_2345_000.001.994C.jpg

Not sure what I'm missing here.

thanks again,

-A
0
 
arober11Commented:
Hi

That URL has a "country name" rather than two character "state code" in it, this should support both formats:

RewriteEngine On
RewriteRule /photos/_[a-zA-Z]*_[a-zA-Z]*_[a-zA-Z]*_[a-zA-Z]*_[a-zA-Z0-9]*_([0-9]*).([0-9]*).([0-9]*C.jpg)   /photos/$1/$2/$3 [L]
0
 
chris0011Author Commented:
Thanks Arober11,

This worked:

RewriteRule /photos/[a-zA-Z0-9+-,'.)(]*_[a-zA-Z0-9+-,')(]*_[a-zA-Z0-9+-,')(]*_[a-zA-Z0-9+-,')(]*_[a-zA-Z0-9+-,']*_[a-zA-Z0-9+-,']*_[a-zA-Z0-9+-,']*_[a-zA-Z0-9]*_([0-9]*)\.([0-9]*)\.([0-9]*C\.jpg)   /photos/$1/$2/$3 [PT,L]

thanks a lot for your help.

-A
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now