Add Trailing Slash for MVC Application

2020-05-09 11:53发布

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!

1条回答
We Are One
2楼-- · 2020-05-09 12:20
RewriteEngine on

RewriteBase /

RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1/ [R=301,L,QSA]
查看更多
登录 后发表回答