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

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

问题描述

我有将 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]

它完美运行,而且重要的是,它永远不会向用户透露真实"URL 是 foo.com/test.txt.

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

现在我想做同样的事情,其中​​真实"的 URL 是 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 中的代码没有正确的工作目录.(它就像 foo.com/index.php 一样运行,css 文件等等都在 foo.com/testdir/中.)也许正确的解决方案是更改 index.php 的内容以引用 testdir/foo.css 而不仅仅是 foo.css 但我很懒.另外,我宁愿重写规则不必知道它是 index.php 或 index.html 还是 what-have-you.

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 中的所有路径.像somedir/style.css"而不是style.css",就像我在直接访问真实"URL 时使用的那样,me.com/somedir

我认为解决这个问题的唯一方法就是放弃并改变这些路径.或者只是将目录somedir"重命名为foo",但由于其他原因我不能这样做.

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 invisible: 当目标是一个文件时工作,而不是当它是一个目录时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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