regex - 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
解决方案
我相信您需要多个规则才能以安全可靠的方式执行此操作:
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。
推荐阅读
- angular - Ionic Cordova 添加原生 Firebase:缺少命令错误
- html - 为什么css在同一个div中以两种不同的方式定义我的文本?
- java - 我在 listView 中有一个倒数计时器,但工作不正常
- javascript - ajax post opens a window with blanked url
- django - 升级到 Django 2.2:AttributeError:模块 'statistics' 没有属性 'pstdev'
- postman - 为什么在我的期望函数中只有一个引号?
- python - 如何从 .txt 文件中的表创建矩阵
- excel - 过滤数据时冻结行/列
- php - 当一切都进入数组时,我们如何为 1 行指定 nl2br()
- php - Yii2中不能对HasMany关系进行排序