我在.htaccess文件中有一段代码:将 PHP 扩展替换为尾部斜杠。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} (.*)/$
RewriteCond %{REQUEST_FILENAME}'.html -f
RewriteRule (.*)/$ $1.html [L]
RewriteCond %{REQUEST_URI} (.*)/$
RewriteCond %{REQUEST_FILENAME}'.php -f
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_URI} (.*)/$
RewriteCond %{REQUEST_FILENAME}'.cgi -f
RewriteRule (.*)/$ $1.cgi [L]
## redirect /dir/foo to /dir/foo/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}'.html -f [OR]
RewriteCond %{REQUEST_FILENAME}'.php -f [OR]
RewriteCond %{REQUEST_FILENAME}'.cgi -f
RewriteRule .* %{REQUEST_FILENAME}/ [R=301,L]
</IfModule>
我的问题是如何将所有请求从www.mydomain.com/foo
重定向到www.mydomain.com/foo/
?(注意尾部斜杠)
解决方案:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.example.com/$1/ [L,R=301]
来源和解释:http://enarion.net/web/htaccess/trailing-slash/
-
RewriteCond %{REQUEST_FILENAME} !-f
如果文件存在,则不重定向 -
RewriteCond %{REQUEST_URI} !(.*)/$
条件:"如果没有尾部斜杠" -
RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]
重定向到 URL + 尾部斜杠