Forum Moderators: phranque

Message Too Old, No Replies

Mod rewrite and image location

         

skylla

12:49 am on Nov 30, 2006 (gmt 0)

10+ Year Member



Hello all,

After a couple of late nights of intense focus on URI rewriting I am now ready to show you my 'code baby' up for some scrutiny. I am not after a ready made fix, but some helpful pointers to adapt the code if neccesary would be great. I can work from there, but keep in mind that until 3 days ago my knowledge was 0. Thanks to all for all previous useful posts&answers and tutorials at webmasterworld.

This is what I wanted:

Rewrite

http://www.example.com/year1969
plus
http://www.example.com/summerof/year1969

to

http://www.example.com/index.php?page=year1969
and
http://www.example.com/index.php?page=summerof_year1969
resp.

And this is how I achieved this:

Options +FollowSymLinks
RewriteEngine on


RewriteCond %{REQUEST_URI}!(\.(gif¦jpg¦css)¦/index\.php)$
RewriteRule ^([^/]+)/?$ /index\.php?page=$1
RewriteRule ^([^/]+)/([^/]+)/?$ /index\.php?page=$1_$2 [L]

Although all my links to imgs and the css are relative to the server I did run into some problems with some of my images not being shown. Only when I moved the images to a deeper directory (from /images/ to /images/images2/ and changed the link correspondingly they were shown [strange].

Could someone shine a light on why only the deeper dir. structure for images would work? Also, any comments on the rewrite will be greatly appreciated.

Thanks

Skylla

jdMorgan

1:56 pm on Nov 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure where your non-working images were originally located, but here's a guess: Because a RewriteCond applies only to the first RewriteRule that follows it, your second rule is unconditional, and will rewrite images in any first-level subdirectory (i.e. /SubdirectoryName/ImageName.ImageType) to /index.php?page=SubdirectoryName_ImageName.ImageType

There are several ways to fix this problem. Here are three of them.

Repeat the RewriteCond:


RewriteCond %{REQUEST_URI} !(\.(gif¦jpg¦css)$¦^/index\.php$)
RewriteRule ^([^/]+)/?$ /index\.php?page=$1
#
RewriteCond %{REQUEST_URI} !(\.(gif¦jpg¦css)$¦^/index\.php$)
RewriteRule ^([^/]+)/([^/]+)/?$ /index\.php?page=$1_$2 [L]

-or-

Stop mod_rewrite processing & exit if image or index.php requested:


RewriteRule \.(gif¦jpg¦css)$¦^index\.php$ - [L]
#
RewriteRule ^([^/]+)/?$ /index\.php?page=$1
RewriteRule ^([^/]+)/([^/]+)/?$ /index\.php?page=$1_$2 [L]

-or-

Skip only these two rules if image or index.php requested; Resume execution of any subsequent rules:


RewriteRule \.(gif¦jpg¦css)$¦^index\.php$ - [S=2]
#
RewriteRule ^([^/]+)/?$ /index\.php?page=$1
RewriteRule ^([^/]+)/([^/]+)/?$ /index\.php?page=$1_$2 [L]

Replace the broken pipe "¦" characters above with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Jim

skylla

9:13 pm on Nov 30, 2006 (gmt 0)

10+ Year Member



Hello Jim,

That's certainly more than I had bargained for! Very neat, especially the third.

Thanks!