为 MVC 应用程序添加尾部斜杠

发布于 2024-10-16 20:46:19 字数 773 浏览 5 评论 0原文

我正在构建一个基于 MVC 设计模式的应用程序,我希望我的 URL 类似于: http:// /example.com/page/action/。我成功地让它与下面的代码一起工作,但如果 URL 不以斜杠结尾,应用程序就会中断。我已经搜索了所有内容,主要是 Stack Overflow,但我还没有找到适合我的代码的好答案。我尝试修改其他人得到的许多答案,但这也不起作用。

这是我的最新代码(不包括我尝试过的代码):

# Turn on Rewrite Engine
    Options +FollowSymLinks
    RewriteEngine On

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

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)/(.*)/$ ./index.php?p=$1&a=$2 [PT]

我从以下位置得到了一些想法:

简单的MVC mod-rewrite

我是mod_rewrite的新手,但我不明白为什么我无法获取添加尾部斜杠的代码。看起来是正确的。有人可以帮我吗?谢谢!

I'm building an application based on the MVC design pattern, and I want my URLS to be like: http://example.com/page/action/. I successfully got it to work with the code below, but if the URL doesn't end with a slash, the application breaks. I've searched all over, mostly Stack Overflow, but I haven't found a good answer that works with my code. I've tried to modify many of the answers others got, but that didn't work either.

Here is my latest code (that doesn't include things I've tried):

# Turn on Rewrite Engine
    Options +FollowSymLinks
    RewriteEngine On

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

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)/(.*)/$ ./index.php?p=$1&a=$2 [PT]

I got a few ideas from:

Simple MVC mod-rewrite

I am new to mod_rewrite, but I don't understand why I can't get the code to add a trailing slash. It looks correct. Can someone help me out? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

酒儿 2024-10-23 20:46:19
RewriteEngine on

RewriteBase /

RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1/ [R=301,L,QSA]
RewriteEngine on

RewriteBase /

RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1/ [R=301,L,QSA]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文