URL重写是消除斜线,一般无视我的规则我的、斜线、重写、规则

2023-09-02 01:02:02 作者:毒霸少女

我在我束手无策,堆栈Overflowers。努力做的事情我认为是一个简单的重写规则与波浪线之后的URL来代替斜线,然后在结尾处添加。html的。 所以,我的.htaccess这样的:

I'm at my wit's end, 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. So my .htaccess is thus:

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]

基本上我运行一个反复的规则,以取代波浪号一下午时间都斜线,那么我的最终规则增加了。html的 - 因为我们所有的网页文件需要在一个文件夹(客户端请求 - 傻,我知道)。

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

我测试模式部分一/部分二/一部分三在这里: http://martinmelin.se/rewrite-rule-tester/ ,它只能如果我砍了最初的斜线和删除重写的条件(这是没有意义的B / C没有文件名我摆在那里应该存在该服务器上),但是这不是我的本地服务器上的情况。

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.

它最终应该读文件部分一〜部分二〜部分three.html,但是当我看到Apache的错误日志在我的本地机器上,我得到这样的:

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:

文件不存在:/路径/要/网站/部分一

File does not exist: /path/to/website/part-one

因此​​,它基本上砍关闭的最后两部分,从来没有尝试添加一个。html的 - 所以在地球上是怎么回事?请帮帮忙,mod_rewrite的大师!

So it basically chops off the final two parts and never tries to add a ".html" -- so what on earth is going on?? Please help, mod_rewrite gurus!!

推荐答案

为什么要你要删除的原因导致 / 是因为重写引擎剥去preFIX(一个URI的前导斜杠),它通过一个htaccess文件规则运行它们。如果你使用Apache 1.3,或者如果规则是在非目录级的服务器或虚拟主机的配置,那么你需要在规则的图案的斜线:

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

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]

此外,你可能不希望在 N 标记,只要你想重写在其当前迭代立即停止。此外,一个条件,首先检查的.html 确实存在,你将改写prevent 500内部服务器错误之前。

Additionally, you probably don't want the N flag, as you want rewrite to stop immediately in its current iteration. Also, a condition which first checks if the .html actually exists before you rewrite will prevent 500 internal server errors.

 
精彩推荐
图片推荐