添加尾随斜线的MVC应用程序 [英] Add Trailing Slash for MVC Application

查看:89
本文介绍了添加尾随斜线的MVC应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立基于MVC设计模式的应用程序,我想我的网址是这样的: HTTP ://example.com/page/action/ 。我顺利地拿到了它与下面的code工作,但如果该URL不以斜杠,应用程序中断结束。我已经找遍了,多堆栈溢出,但我没有发现我的code工作一个很好的答案。我试图改变许多人得到了答案,但也不能工作。

下面是我最新的code(不包括的东西我已经试过):

 上重写引擎#开启
    选择了FollowSymLinks +
    RewriteEngine叙述在    重写规则^(。*)/ $ 1 / [R = 301,L]    的RewriteCond%{} REQUEST_FILENAME!-d
    的RewriteCond%{} REQUEST_FILENAME!-f
    重写规则^(。*)/(*)/ $ ./index.php?p=$1&a=$2 [PT]

我有一些想法来自:

简单MVCmod-rewrite

我是新来mod_rewrite的,但我不明白为什么我不能得到code以添加一个尾随斜线。它看起来是正确的。有人可以帮我吗?谢谢!


解决方案

  RewriteEngine叙述上的RewriteBase /的RewriteCond%{REQUEST_URI}!/ $
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则(。*)$ 1 / [R = 301,L,QSA]

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!

解决方案

RewriteEngine on

RewriteBase /

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

这篇关于添加尾随斜线的MVC应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆