mod_rewrite的无形:当目标是一个文件,而不是当它是一个目录工作 [英] Mod_rewrite invisibly: works when target is a file, not when it's a directory

查看:97
本文介绍了mod_rewrite的无形:当目标是一个文件,而不是当它是一个目录工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有变成foo.com/test(或foo.com/test/)到foo.com/test.txt这个重写规则:

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]

它完美,更重要的,从不显露对真实的网址是foo.com/test.txt用户。

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

现在我想要做同样的事情,其中​​的真实的网址是foo.com/testdir,其中TESTDIR是一个目录:

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.

谁能解释一下Apache是​​想在这里,最好的解决办法?只是重写TESTDIR / index.php文件不适合我,因为在随后的index.php的code工作没有合适的工作目录。 (它运行就好像它是foo.com/index.php和css文件和诸如此类的东西都是foo.com/testdir/)或许正确的解决办法是改变的index.php内容指TESTDIR / foo中。 CSS,而不是仅仅foo.css但我很懒。另外,我宁愿重写规则并不需要知道它是否是的index.php或index.html或者什么具备的,你。

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.

由于第一个答案,我现在看到我的问题的本质是:如果我希望用户使用的URL me.com/foo,并获得了真正的URL me.com/somedir然后我要挑两者之间不良的东西:

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. 的URL被重写明显(用户可以看到me.com/somedir)。

  2. 在somedir的东西运行,如果是在根目录下。所以,我必须改变somedir / index.php中所有的路径,例如。像不像我以前去直接真正的网址时,me.com/somedir
  3. somedir / style.css文件,而不仅仅是style.css文件
  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

我想这唯一的答案就是把它吸上来,改变这些路径。或者只是改名目录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.

推荐答案

添加了结尾的斜线:

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

http://foo.com/testdir 是不是一个有效的URL的目录,Apache的寻找一个名为TESTDIR,因为TESTDIR实际上是一个目录的Apache试图通过重定向到TESTDIR /修复。所以,当你的工作重写随后失踪斜线错误固定通过重定向到TESTDIR /.

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/.

您可能还需要一个基地href标记添加到您的文档的头部或相对链接将打破:

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

<base href="/">

这篇关于mod_rewrite的无形:当目标是一个文件,而不是当它是一个目录工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆