Forum Moderators: phranque
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
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
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
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