简体   繁体   English

在结尾的网址中添加斜杠

[英]add trailing slash to slug url

I have this rewrite condition I use it to grab the slug url and search for a blog post like this: 我有这样的重写条件,我用它来获取条形网址并搜索这样的博客文章:

http://localhost:8080/blog/my-first-post

So I grab the slug and then look for the post and display it. 因此,我抓住了弹头,然后寻找并显示该帖子。 Right now it accepts this two forms 现在它接受这两种形式

http://localhost:8080/blog/my-first-post
http://localhost:8080/blog/my-first-post/

How should I change the .htaccess so it always adds or redirects with the slash at the end. 我应该如何更改.htaccess,以便它始终以末尾的斜杠添加或重定向。

And how should I change it to remove the slash always? 我应该如何更改它以始终删除斜杠?

Here is the .htaccess I have now which I have it in the blog directory: 这是我现在在博客目录中拥有的.htaccess文件:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

You can replace your current htaccess code by this one 您可以用此代码替换当前的htaccess代码

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /blog/

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

    # remove trailing slash
    #RewriteCond %{REQUEST_FILENAME} !-d
    #RewriteRule ^(.+?)/$ $1 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

Right now, it adds trailing slash. 现在,它会添加斜杠。 If you want to remove it, comment add trailing slash block lines and uncomment remove trailing slash block lines 如果要删除它,请注释 add trailing slash行,并取消注释 remove trailing slash

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM