Forum Moderators: phranque

Message Too Old, No Replies

Rewriting two very similar URLs

using the NOT character to distinguish between the two

         

maerk

10:26 am on Oct 9, 2005 (gmt 0)

10+ Year Member



I need to be able to rewrite two types of URLs on my photo gallery which uses a simulated directory structure:

http://example.com/gallery/food/cheese/stilton.jpg
http://example.com/gallery/food/cheese

Because the two types of URL begin in the same way, I need a way to distinguish between the two in my RewriteCond's. Not only that, but I need to be able NOT to rewrite direct requests to the php file which the URL is rewritten to. In other words, the URL
http://example.com/gallery/index.php?dir=./food/cheese should not be rewritten.

I've been trying to do something with the not character, but I don't think I'm getting too far:

# To prevent rewriting of strings containing ".php" or ".jpg"
RewriteCond !.*\.php.*$
RewriteCond !.*\.jpg$

# To rewrite requests for a folder
RewriteRule (.*)/(.*) index.php?dir=./$1/$2 [L]

# To rewrite requests for a file
RewriteRule ^(.*)\.jpg$ index.php?file=./$1.jpg

jd01

6:12 pm on Oct 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi maerk,

Welcome to WebmasterWorld.

I am not sure I understand your logic, or what you need... You have a rule that says to 'not rewrite .jpg files' and a later rule that says to rewrite them, which is it you really need?

RewriteRule ^([^.]+)/?$ /index.php?variable=$1 [L]

If you have three levels of your site, you will need something more like this:
RewriteRule ^([^/]+/)?([^/]+/)?([^.]+)/?$ /index.php?variable=$3 [L]

See this recent thread [webmasterworld.com] for more on the discussion of the rules.

As far as .jpg files go, the best guess I have is you need them to be rewritten, as long as they are not in a query_string? -- If this is the case, you will not need the negative rule, because mod_rewrite does not 'see' a query string, unless explicitly told to do so, through the use of a QUERY_STRING condition. I also do not know if you will need the full path to the file passed to the php file, or only the image name?

I also strongly recommend you visit the Charter [webmasterworld.com] and Apache Forum Library [webmasterworld.com] for more information on mod_rewrite...

Hope this give you some ideas.

Justin

maerk

7:28 pm on Oct 9, 2005 (gmt 0)

10+ Year Member



Thanks for your reply! I didn't know the mod_rewrite ignores query strings, that makes things a lot easier!

Maybe the best way to explain what I'm trying to is to show how the URLs need to be rewritten:

A request for a directory listing...
http://example.com/gallery/food/cheese/
should be rewritten to
http://example.com/gallery/index.php?dir=./food/cheese
(with no trailing slash in the query string!)

A request to view a JPG file...
http://example.com/gallery/food/cheese/stilton.jpg
should be rewritten to
http://example.com/gallery/index.php?file=./food/cheese/stilton.jpg

I've read and re-read everything that I can find on mod_rewrite, including the docs at the apache site, but I can't seem to get things to work!

I hate to be asking for tech support, but I really have tried everything and I'm completely lost :S

jd01

7:40 pm on Oct 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should be able to use something close to what is in my previous post:

RewriteRule ^([^/]+/)?([^/]+/)([^./]+)/?$ /index.php?variable=.$1$2$3 [L]
RewriteRule ^([^/]+/)?([^/]+/)?([^/]+/)([^.]+\.jpg)$ /index.php?variable=.$1$2$3$4 [L]

This should work for:
/dir/name
/dir/name/
/dir/dir/name
/dir/dir/name/
/name/pic.jpg
/dir/name/pic.jpg
/dir/dir/name/pic.jpg

I have not tested it, so there may be some adjusting necessary, but it will get you close.

Justin

maerk

1:03 am on Oct 10, 2005 (gmt 0)

10+ Year Member



Thank you, yes it did work with a tiny bit of persuasion:

RewriteRule ^([^/]+/)?([^/]+/)?([^./]+)/?$ /gallery/index.php?variable=./$1$2$3 [L]
RewriteRule ^([^/]+/)?([^/]+/)?([^/]+/)([^.]+\.jpg)$ /gallery/index.php?variable=$1$2$3$4 [L]

That's wonderful, thank you very much!