重写以添加尾部斜杠,但独立于域?

Rob*_*ght 4 apache url mod-rewrite rewrite

我正在使用Apache和mod_rewrite重写我的网络应用程序的URL.你可以在这里看到它:

RewriteEngine On
RewriteBase / 

# www. to non-www.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# Redirect non-existant files so there's a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [R=301,L]

# Send the URL to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA]
Run Code Online (Sandbox Code Playgroud)

一切正常,但问题是尾随斜线重写.它在我位于域的根目录时有效,但在我的暂存环境中,我在子目录中运行此应用程序.我必须修改RewriteBase指令以包含子目录或重写失败.

我正在寻找一个解决方案,它将为URL添加一个尾部斜杠 - 无论应用程序是否在服务器的根目录上运行,而无需更改RewriteBase.提前致谢.

Rob*_*ght 5

经过@MortBM的探索和帮助之后,看起来下面的效果很好:

RewriteEngine On

# www. to non-www.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# Add a trailing slash to any non-existent files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ %{REQUEST_URI}/ [R=301,L]

# Send the URI to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [QSA,L]
Run Code Online (Sandbox Code Playgroud)

总结:删除RewriteBase,并%{REQUEST_URI}在重定向内使用它:)