.htaccess 在重写规则中强制将slach添加到末尾

.htaccess 在重写规则中强制将slach添加到末尾,.htaccess,mod-rewrite,s3-rewrite-rules,.htaccess,Mod Rewrite,S3 Rewrite Rules,我正在使用.htaccess下的mod_rewrite,并试图在url末尾添加slach“/”: 解释此url:http://domain/ABC/DEF像这样http://domain/ABC/DEF/ 如何编写规则?若要在请求\u uri的末尾添加尾随斜杠,可以在htaccess中使用以下规则: RewriteEngine On #If there is already a trailing slash, skip the rule. RewriteCond %{REQUEST_URI} !

我正在使用.htaccess下的mod_rewrite,并试图在url末尾添加slach“/”: 解释此url:
http://domain/ABC/DEF
像这样
http://domain/ABC/DEF/


如何编写规则?

若要在请求\u uri的末尾添加尾随斜杠,可以在htaccess中使用以下规则:

RewriteEngine On
#If there is already a trailing slash, skip the rule.
RewriteCond %{REQUEST_URI} !/$ 
#else redirect any request to add a trailing slash
RewriteRule ^(.+)$ /$1/ [L,R]
这会自动添加尾部斜杠并更改url

重写条件在这里很重要,以避免重定向循环错误,如果没有此条件,/foo请求在初始迭代时重定向到/foo/,在第二次迭代时,目标url/foo/与重写模式匹配,并重定向/foo/到/foo/,并导致循环错误

(希望这有帮助!)