views:

108

answers:

1

I have this rewrite rule that turns foo.com/test (or foo.com/test/) into foo.com/test.txt:

RewriteRule ^test/?$ test.txt [NC,L]

It works perfectly and, importantly, never reveals to the user that the "real" URL is foo.com/test.txt.

Now I want to do the same thing where the "real" URL is foo.com/testdir, where testdir is a directory:

RewriteRule ^test2/?$ testdir [NC,L]

It does the rewrite but it shows that rewrite to the user. Not what I want.

Can anyone explain what Apache is thinking here, and the best solution? Just rewriting to testdir/index.php doesn't work for me since then the code in index.php doesn't have the right working directory. (It runs as if it were foo.com/index.php and the css files and whatnot are in foo.com/testdir/.) Perhaps the right solution is to change the contents of index.php to refer to testdir/foo.css instead of just foo.css but I'm lazy. Also, I'd rather the rewrite rule didn't have to know whether it's index.php or index.html or what-have-you.

Addendum

Thanks to the first answer, I now see the essence of my problem: If I want the user to use the URL me.com/foo and get to the "real" URL me.com/somedir then I have to pick between two undesirable things:

  1. The URL gets visibly rewritten (the user can see me.com/somedir).
  2. The stuff in somedir runs as if it's in the root directory. So I have to change all the paths in somedir/index.php, for example. Like "somedir/style.css" instead of just "style.css" like I used when going straight to the "real" URL, me.com/somedir

I'm thinking the only answer to this is to suck it up and change those paths. Or just rename the directory "somedir" to "foo" but I can't do that for other reasons.

+1  A: 

Add the trailing slash:

RewriteRule ^test2/?$ testdir/ [NC,L]

http://foo.com/testdir isn't a valid url to the directory, Apache looks for a file called testdir, and because testdir is actually a directory Apache tries to fix it by redirecting to testdir/ . So while your rewrite worked the subsequent missing slash error was fixed by redirecting to testdir/.

You may also need to add a base href tag to the head of your document or relative links will break:

<base href="/">
Rob
That doesn't have any effect for me. I see what you mean about the slash and it's a good idea to add it, but it seems to not solve the problem I'm having.
dreeves
I just tested this with Apache 2.2 and it worked fine... make sure you clear your cache.
Rob
Check out the addendum I just added to the question. Adding the slash does make the rewrite work but then you have to change all the paths inside testdir. Perhaps there's just no way around that. Thanks a lot for this answer, btw.
dreeves
Oh ok I understand, if you're using relative links you can use a <base href="/"> tag in the head and just make all your links relative to the document root.
Rob
Ah, the base tag in the header is a great idea. Want to add that to your answer and I'll mark it as the accepted answer?
dreeves
No problem, added to the answer.
Rob