从 htaccess 重定向中删除尾部斜杠

时间:2021-04-06 20:15:32

标签: .htaccess

我想使用 htaccess 进行重定向,但遇到尾随斜杠的问题。

我想从 https://test.com/transferhttps://test.com/transfer/ 重定向 https://test2.com/destination 下面是我的 htaccess 规则,但它似乎只适用于 https://test.com/transfer 而不是 https://test.com/transfer/

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI}  ^/transfer$ [NC]
RewriteRule ^ https://somesite.com/destination/$1 [R,L]

我的规则有什么问题?

1 个答案:

答案 0 :(得分:2)

试试:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}  ^/transfer/?$ [NC]
RewriteRule ^ https://somesite.com/destination/$1 [R,L]

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/transfer/?$ https://somesite.com/destination/ [NC,R,L]

确保您只使用 RewriteEngine On 一次。

如果您想重定向 transfer 中的所有内容:如果 transfer 是一个目录 btw,请使用:

RewriteEngine on
RewriteRule ^/transfer/?(.+)?$ https://somesite.com/destination/$1 [R, L]
相关问题