簡體   English   中英

在結尾的網址中添加斜杠

[英]add trailing slash to slug url

我有這樣的重寫條件,我用它來獲取條形網址並搜索這樣的博客文章:

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

因此,我抓住了彈頭,然后尋找並顯示該帖子。 現在它接受這兩種形式

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

我應該如何更改.htaccess,以便它始終以末尾的斜杠添加或重定向。

我應該如何更改它以始終刪除斜杠?

這是我現在在博客目錄中擁有的.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>

您可以用此代碼替換當前的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>

現在,它會添加斜杠。 如果要刪除它,請注釋 add trailing slash行,並取消注釋 remove trailing slash

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM