regex - 在 htaccess 中用连字符替换 %20

标签 regex apache .htaccess mod-rewrite

以下用连字符替换 %20 的代码运行不正常。有时有效,有时无效,如果标题中有数字,则数字后的空格不会被替换。有什么帮助吗? 例如。
他今年30岁
他是 30%20years%20old

RewriteEngine On
RewriteCond %{THE_REQUEST} (\s|%20)
RewriteRule ^([^\s%20]+)(?:\s|%20)+([^\s%20]+)((?:\s|%20)+.*)$ $1-$2$3 [N,DPI]
RewriteRule ^([^\s%20]+)(?:\s|%20)+(.*)$ /$1-$2 [L,R=301,DPI]

最佳答案

代替那些复杂的替换,尝试使用这 2 条规则用连字符替换所有空格:

RewriteEngine On

RewriteRule "^(\S*)\s+(\S*)$" /$1-$2 [L,NE,R=302]

RewriteRule "^(\S*)\s+(\S*\s+.*)$" $1-$2 [L]

# remove multiple hyphens
RewriteRule ^(.*)-{2,}(.*)$ /$1-$2 [L,R=302]

关于regex - 在 htaccess 中用连字符替换 %20,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32746297/

相关文章:

java - 正则表达式删除任何 ipv6 地址的前导零

python - 从 Python 中的字符串中提取数字和大小信息(KB、MB 等)

javascript - 在javascript中对字符串进行正则表达式匹配

html - 在 apache 本地主机上添加内容

android - Android 上的简单 Web 服务器

Apache2 未安装/etc/apache2 ubuntu 12.04

apache - htaccess 重定向到 https ://www

regex - Perl:如何将正则表达式匹配大写?

.htaccess - 使用 SSL 将 www 重定向到非 www

php - 编写多语言网站 : the webdesigner's (best) options