Forum Moderators: phranque

Message Too Old, No Replies

rewrite directory to file.php

/subdirname/more/stuff to /subdirname.php

         

ergophobe

11:30 pm on Jan 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It seems like this should be incredibly simple, but I just can't get this to work.

let's say I have this path to a file on the filesystem

/webroot/dir1/filename.php

and I have this url

example.com/dir1/filename/more/stuff

I want to rewrite to

example.com/dir1/filename.php/more/stuff

So I have a simple rewrite rule that works fine

RewriteBase /dir1
RewriteRule (.*)/filename/(.*) $1file.php$2 [L]

It finds /webroot/dir1/file.php just as I want.

The problem is, as soon as I put another file in that same directory dir1 that has the same basename, but a different extension (e.g file.css), the server tries to find it instead, but rewrites to

/webroot/dir1/filename.css

Even if I put in a RewriteCond like

RewriteCond %{REQUEST_URI}!.*\.(css¦gif¦jpg).*

it doesn't seem to help. I've tried a bunch of permutations, but I just can't get it to do what I want.

coopster

6:59 pm on Jan 22, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Looks like content negotiation is stepping in. Have you tried dropping the ".*" off the end of your RewriteCond?

olwen

7:30 pm on Jan 22, 2005 (gmt 0)

10+ Year Member



I don't use a rewrite. My script is filename with no extension.
In .htaccess I have
<Files filename>
ForceType application/x-httpd-php
</Files>

Apache2 also needs

acceptpathinfo on

The first few lines of the script "filename" parse the parameters

$dirname = getenv("REQUEST_URI");
if (substr($dirname,-1,1)!= "/") {
$dirname = $dirname."/";
}
$dirname = split("/", $dirname);

I have a bit of stuff that checks it's the correct form as well. You can figure that out because it's going to be specific.

jdMorgan

8:05 pm on Jan 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd try getting rid of RewriteBase, and putting /dir1 in both the pattern and the substitution. If this fails, your error log will show the path that the server tried to use to find the substituted resource. You then figure out what part of the path is missing and caused the failure, and put that into RewriteBase. I've never had to use RewriteBase on any properly-configured server... Not sure if that is just luck or what.

Jim

ergophobe

3:36 am on Jan 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks for the suggestions guys. I had tried all those things already. The Forcetype solution worked, but that wasn't really what I was after. The others didn't help. Sometimes it's too obvious though...


I've never had to use RewriteBase on any properly-configured server... Not sure if that is just luck or what.

I tried it on another server and sure enough... success.

This set of rules covers all the cases I can think of and obviates the need for the RewriteCondition

RewriteEngine On
RewriteRule (.*)farmers/farm-dir/(.*) $1farmers/farm-dir.php/$2 [L]
RewriteRule (.*)farmers/farm-dir$ $1farmers/farm-dir.php [L]
RewriteRule (.*)farmers/farm-dir\?(.*) $1farmers/farm-dir.php?$2 [L]

I'm still not sure why that doesn't work on the first server. It's so simple and other rewrites have worked fine, but for whatever reason, it just doesn't work there. Since the non-working server is just my "playstation", I don't really care.