Rewrite and static files

Hello everyone,

1- I want to create friendly URLS which redirects:

“/article/123/whatever.html”
to
/article.php?id=123

I use this rule:

RewriteRule ^article/([1-9][0-9]{1,4})/(.*?).html$ ./article.php?id=$1

However images,css fie etc (anything other than .html) should be redirected to correct place. For example “/article/123/images/picture.jpg” should be redirected to “images/picture.jpg” and “/article/123/site.css” should be redirected to “site.css”.

what rule do I need? I treied a few but I receive server error :slight_smile:

2- Where can I find an easy tutorial on regular expressions (specifically with Mod Rewrite format)?

I appreciate your time and help.

Thanks.

sarmadys,

Regards,

DK

Ok, I read your tutorial and liked it very much. However I would prefer not to use full URL or base address for images. Now I want :

1- page redirection

/article/26/categoryname/articlename

redirected to:

/article.php?id=26

2- images,css and javascripts to be redirected to their correct path. i.e.

/article/26/categoryname/articlename/images/logo.jpg -> /images/logo.jpg
/article/26/categoryname/articlename/userpics/user10.jpg -> /userpics/user10.jpg
/article/26/categoryname/articlename/site.css -> /site.css

I have written below rules basedon your tutorial. However image etc rules does not work correctly. Is therea way to fix it (instead of using base or explicit path for images etc.)

RewriteEngine on

RewriteRule ^article/([0-9]+)/([a-zA-Z_]+)/([0-9a-zA-Z_]+)$ article.php?id=$1 
RewriteRule ^article/([0-9]+)/([a-zA-Z_]+)/([0-9a-zA-Z_]+)/([a-z]+)/(.*?).(ico|gif|jpg|png)$ $4/$5.$6  [L]

Thank you very much again for looking into this and help.

Mac

mac,

Okay, I recommended the <base> tag over the absolute links (internal over external) and you’ve come up with an obvious third alternative: Using mod_rewrite to redirect images/{whatever}.jpg to {DOCUMENT_ROOT}/images/{whatever}.jpg. HOWEVER, since that’s merely a modification of the absolute option (and a greater load on the server to no useful purpose), I’ll stick with my recommendation (use the <base> tag).

Regards,

DK

Hi,

Thanks again for your time and help.

I also thought about server load but I was assuming it is not that considerable. Now that I think, it will need a rewrite for every single image on the site (multiple times more than page requests).

I’ll go with your recommendation.

Regards, mac