.htaccess mod_rewrite 添加“/”到网址末尾

发布于 2024-11-06 09:46:44 字数 370 浏览 0 评论 0原文

这是我的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抱着落日 2024-11-13 09:46:44

虽然我不知道您问题的答案,但我会注意到您的问题和代码的两个奇怪之处,这些奇怪之处可能与当前的问题有关。

  1. 使用代码中的 RewriteBase,这些规则甚至不应该被触发。

  2. 虽然我自己是正则表达式的新手,但我查看 ([^/]+) 并对您捕获它的原因感到有点困惑。我知道 ^ 匹配字符串的开始,但这永远不会是真的,因为在字符串的真正开始处已经有了另一个。

话虽这么说,我可能会编写如下代码:

RewriteBase /moduli/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ $1/index.php/$2 [L]

这将重写 URL,如下所示:

http://www.website.com/moduli/novice/view
http://www.website.com/moduli/novice/index.php/view

根据您的代码块,这似乎就是您想要做的事情。如果不是,那么我很抱歉。

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.

  1. With the RewriteBase you have in your code, those rules should not even be being triggered.

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

RewriteBase /moduli/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ $1/index.php/$2 [L]

This would rewrite URLs as below:

http://www.website.com/moduli/novice/view
http://www.website.com/moduli/novice/index.php/view

Based on your block of code, this seems to be what you are trying to do. If it is not, then I am sorry.

寻找一个思念的角度 2024-11-13 09:46:44

我不认为这与你的重写规则有关(它不匹配)。

添加 / 是因为当您请求 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 detects zz is a directory, it transforms it to http://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 of RewriteCond %{REQUEST_FILENAME} !-d)

BTW, I don't quite understand your RewriteBase /kajak/ line there - are you sure it's correct?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文