简体   繁体   English

Apache mod_rewrite正在删除斜杠,并且通常会忽略我的规则

[英]Apache mod_rewrite is removing slashes and generally ignoring my rules

I'm at my wit's end, Stack Overflowers. 我机智的是,Stack Overflowers。 Trying to do what I thought was a simple rewrite rule to replace slashes in the URL with tilde, then add a ".html" at the end. 尝试执行我认为是简单的重写规则,以用代字号替换URL中的斜杠,然后在末尾添加“ .html”。 So my .htaccess is thus: 所以我的.htaccess是这样的:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.+)/(.+)$ $1~$2 [N]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([^/]+)$ $1.html [L]

Basically I'm running a repeated rule to replace all slashes with tildes one at time, then my final rule adds the ".html" -- because all our web files need to be in one folder (client request--silly, I know). 基本上,我正在运行一条重复的规则,一次用波浪号替换所有斜杠,然后我的最终规则添加了“ .html”-因为我们所有的Web文件都需要放在一个文件夹中(客户端请求-很傻,我知道)。

I've tested the pattern "part-one/part-two/part-three" here: http://martinmelin.se/rewrite-rule-tester/ and it only works if I chop off the initial slash and remove the rewrite conditions (which makes no sense b/c no filename I put in there should exist on that server), but that's not the case on my local server. 我已经在这里测试了模式“ part-one / part-two / part-three”: http : //martinmelin.se/rewrite-rule-tester/ ,并且只有当我切掉初始斜杠并删除重写后,它才有效条件(没有理由我在其中放置的文件名不存在b / c应该在该服务器上存在),但是在我的本地服务器上不是这种情况。

It should eventually read the file "part-one~part-two~part-three.html" but when I look at the Apache error log on my local machine, I get this: 它最终应该读取文件“ part-one〜part-two〜part-three.html”,但是当我在本地计算机上查看Apache错误日志时,会得到以下信息:

File does not exist: /path/to/website/part-one 文件不存在:/ path / to / website / part-one

So it basically chops off the final two parts and never tries to add a ".html" -- so what on earth is going on?? 因此,它基本上剔除了最后两部分,并且从不尝试添加“ .html”-那么到底是怎么回事? Please help, mod_rewrite gurus!! 请帮忙,mod_rewrite大师!

The reason why it wants you to remove the leading / is because the rewrite engine strips off the prefix (the leading slash of an URI) before it runs them through rules in an htaccess file. 之所以要您删除开头的/是因为重写引擎在通过htaccess文件中的规则运行前缀之前先去除了前缀(URI的前斜杠)。 If you were using apache 1.3 or if the rules were in a non-per-directory context in the server or vhost config, then you'd need the leading slash in the rule's pattern: 如果您使用的是Apache 1.3,或者规则位于服务器或vhost配置中的非按目录上下文中,则需要在规则模式中使用斜杠:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/(.+)$ /$1~$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^/]+)$ /$1.html [L]

Additionally, you probably don't want the N flag, as you want rewrite to stop immediately in its current iteration. 此外,您可能不希望N标志,因为您希望重写在当前迭代中立即停止。 Also, a condition which first checks if the .html actually exists before you rewrite will prevent 500 internal server errors. 同样,在重写之前先检查.html实际是否存在的条件将防止500个内部服务器错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM