.htaccess mod_rewrite 添加“/”到网址末尾
这是我的 .htaccess 代码:
RewriteBase /kajak/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^moduli/([^/]+)/(.*)$ moduli/$1/index.php/$2 [L]
现在 /
已附加到每个 URL。例如,http://127.0.0.1/moduli/novice
变为http://127.0.0.1/moduli/novice/
。
如何防止最后出现 /
?
This is my .htaccess code:
RewriteBase /kajak/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^moduli/([^/]+)/(.*)$ moduli/$1/index.php/$2 [L]
Now /
is appended to every URL. For example, http://127.0.0.1/moduli/novice
becomes http://127.0.0.1/moduli/novice/
.
How can I prevent getting /
at the end?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然我不知道您问题的答案,但我会注意到您的问题和代码的两个奇怪之处,这些奇怪之处可能与当前的问题有关。
使用代码中的 RewriteBase,这些规则甚至不应该被触发。
虽然我自己是正则表达式的新手,但我查看 ([^/]+) 并对您捕获它的原因感到有点困惑。我知道 ^ 匹配字符串的开始,但这永远不会是真的,因为在字符串的真正开始处已经有了另一个。
话虽这么说,我可能会编写如下代码:
这将重写 URL,如下所示:
根据您的代码块,这似乎就是您想要做的事情。如果不是,那么我很抱歉。
While I do not know the answer to your question, I will note two oddities about your question and your code that may be related to the problem at hand.
With the RewriteBase you have in your code, those rules should not even be being triggered.
While I am new to regex myself, I look at ([^/]+) and am a little confused as to why you are capturing it. I know that ^ matches the START of the string, which would never be true since you already have another one at the real start of the string.
This being said, I would probably write the code as below:
This would rewrite URLs as below:
Based on your block of code, this seems to be what you are trying to do. If it is not, then I am sorry.
我不认为这与你的重写规则有关(它不匹配)。
添加 / 是因为当您请求
http://example.com/xx/zz
并且 Web 服务器检测到zz
是目录时,会将其转换为http://example.com/xx/zz/
通过 301 重定向(浏览器发出另一个请求 - 检查 apache 日志)。此处了解尾随斜杠重定向 。
你必须问自己,当请求的 url 是
http://127.0.0.1/moduli/novice/
时你希望发生什么(你希望它被你的重定向捕获还是不是吗?目前它没有被捕获,因为RewriteCond %{REQUEST_FILENAME} !-d
)顺便说一句,我不太明白你的
RewriteBase /kajak/
行 - 你确定它是正确的吗?I don't think that's related to your rewrite rule, (it does not match it).
The / is added because when you request
http://example.com/xx/zz
and the web server detectszz
is a directory, it transforms it tohttp://example.com/xx/zz/
through a 301 redirect (the browser makes another request - check you apache logs).Read about the trailing slash redirect thing here.
The, you must aks yourself, what do you want to happen when the url requested is
http://127.0.0.1/moduli/novice/
(Do you want it to be be catched by your redirect or not? Currently it's not catched because ofRewriteCond %{REQUEST_FILENAME} !-d
)BTW, I don't quite understand your
RewriteBase /kajak/
line there - are you sure it's correct?