删除斜杠用的mod_rewrite斜杠、mod_rewrite

2023-09-02 09:42:43 作者:北方故事

我已经试过每一个例子,我能找到的,它们都产生一个内部服务器错误。我有这些规则设置(这工作,没有错误):

I've tried every single example I could find, they all produce an internal server error. I have these rules set up (this works, no error):

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule ^((/?[^/]+)+)/?$ ?q=$1 [L]

所以,如果它不是现有的文件或与我们重定向index.php文件的现有目录。例如, http://domain.com/foo/bar 变成 http://domain.com/?q=foo/bar

事情是,我想最后的斜线剥离。于是脱下 / 的统治结束。我该如何让这个 http://domain.com/foo/bar/ 变成 http://domain.com/foo/bar 一个可见的重定向第一(固定客户端的URL),只有,然后真正的,无声的重定向到] Q =

Thing is, I want the trailing slash stripped. So take off the /? at the end of the rule. How do I make it so that http://domain.com/foo/bar/ becomes http://domain.com/foo/bar with a visible redirect first (fixing the client's URL), and only then the real, silent redirection to ?q=?

我到处看看我看到这样的:

Everywhere I look I see this:

RewriteRule (.*)/$ $1 [R,L]

但它给了我一个500错误,如果我我的规则之前插入。

But it gives me a 500 error if I insert it before my rule.

推荐答案

如果富/酒吧存在作为一个真正的目录,那么服务器会被重新定向客户端富/酒吧/(带斜线)。它这样做,为了使相对URL在客户端上正常工作。如果你把一个规则重写回富/酒吧重定向那么就会出现一个循环。一个简单的方法来测试,如果发生的事情是指定根本不存在的路径(我从你的index.php检测假设目录树中确实存在)。不存在的路径不会触发内置的重定向。

If foo/bar exists as a real directory, then the server will be redirecting the client to foo/bar/ (with the trailing slash). It has to do that in order for relative URLs to work correctly on the client. If you put in a rule to rewrite that back to foo/bar with a redirect then there will be a loop. An easy way to test if that's happening is to specify a path that doesn't exist at all (I assume from your index.php detection that the directory tree actually exists). The nonexistent path won't trigger the built-in redirect.

如果我安装了类似的一套规则,以你(以及建议的斜线去除规则),我可以看到,存在一个没有目录和1之间的区别。那些预期不工作,那些确实导致Firefox说的该页面未正确重定向的。 IE8说类似的东西。也许你正在使用可以检测到它和Apache设置使得它成为了500错误?

If I setup a similar set of rules to yours (plus the suggested slash-removal rule) I can see the difference between a directory that exists and one that doesn't. The ones that don't work as expected, the ones that do cause Firefox to say This page isn't redirecting properly. IE8 says something similar. Perhaps the Apache setup you're using can detect it and turns it into the 500 error?