首页 > 解决方案 > Regex wildcard page rule for redirecting trailing slash

问题描述

trying to redirect all trailing slash urls to non trailing slash on Cloudflare using a redirect page rule. I’m new to regex, can anyone help? Thanks

Tried: example.com/

To

*example.com/$2

But had no idea what I was doing

标签: regexcloudflare

解决方案


我相信您需要多个规则才能以安全可靠的方式执行此操作:

https://example.com/*/ to https://example.com/$1 
https://example.com/*/?* to https://example.com/$1?$2

第一个页面规则用于重定向带有斜杠的子页面;第二个用于使用查询字符串重定向子页面。基本 URL 可能需要相同的内容

https://example.com/ to https://example.com 
https://example.com/?* to https://example.com?$1 

但是,如果您的网站在 Apache 上运行,您最好使用.htaccess文件,如下所示:

RewriteEngine On
# Redirect non-directories to URL without trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]

如果您不关心目录,只需删除 RewriteCond。


推荐阅读