如何删除URI中包含'PREG'或'HTACCESS'的多个斜杠site.com/edition/new/// -〉site.com/edition/new/site.com/edition///new/ -〉site.com/edition/new/谢谢
tsm1rwdh1#
$url = 'http://www.abc.com/def/git//ss'; $url = preg_replace('/([^:])(\/{2,})/', '$1/', $url); // output http://www.abc.com/def/git/ss $url = 'https://www.abc.com/def/git//ss'; $url = preg_replace('/([^:])(\/{2,})/', '$1/', $url); // output https://www.abc.com/def/git/ss
j9per5c42#
在正则表达式中使用加号+意味着前面的一个或多个字符的出现。所以我们可以在preg_replace中添加它,以将一个或多个/的出现替换为其中之一
+
/
$url = "site.com/edition/new///"; $newUrl = preg_replace('/(\/+)/','/',$url); // now it should be replace with the correct single forward slash echo $newUrl
3pvhb19x3#
简单,看看这个例子:
$url ="http://portal.lojav1.local//Settings////messages"; echo str_replace(':/','://', trim(preg_replace('/\/+/', '/', $url), '/'));
输出:
http://portal.lojav1.local/Settings/messages
pexxcrt24#
编辑:哈我把这个问题读成“没有preg”哦好吧:3
function removeabunchofslashes($url){ $explode = explode('://',$url); while(strpos($explode[1],'//')) $explode[1] = str_replace('//','/',$explode[1]); return implode('://',$explode); } echo removeabunchofslashes('http://www.site.com/edition////new///');
qvtsj1bj5#
http://domain.com/test/test/〉http://domain.com/test/test
# Strip trailing slash(es) from uri RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+?)[/]+$ $1 [NC,R,L]
http://domain.com//test//test//〉http://domain.com/test/test/
# Merge multiple slashes in uri RewriteCond %{THE_REQUEST} ^[A-Z]+\ //*(.+)//+(.*)\ HTTP RewriteRule ^ /%1/%2 [R,L] RewriteCond %{THE_REQUEST} ^[A-Z]+\ //+(.*)\ HTTP RewriteRule ^ /%1 [R,L]
如果测试后一切正常,则将R更改为R=301...有谁知道如何使用上述方法在查询中保留双斜杠?(For例如:/test//test//?test=test//test〉/test/test/?test=test//test)
zyfwsgd66#
domain.com//test//test/?test/test 〉网站domain.com/test/test/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ //*(.+)//+(.*)\ HTTP RewriteRule ^ /%1/%2? [R,L] RewriteCond %{THE_REQUEST} ^[A-Z]+\ //+(.*)\ HTTP RewriteRule ^ /%1? [R,L]
6条答案
按热度按时间tsm1rwdh1#
j9per5c42#
在正则表达式中使用加号
+
意味着前面的一个或多个字符的出现。所以我们可以在preg_replace中添加它,以将一个或多个/
的出现替换为其中之一3pvhb19x3#
简单,看看这个例子:
输出:
pexxcrt24#
编辑:哈我把这个问题读成“没有preg”哦好吧:3
qvtsj1bj5#
http://domain.com/test/test/〉http://domain.com/test/test
http://domain.com//test//test//〉http://domain.com/test/test/
如果测试后一切正常,则将R更改为R=301...
有谁知道如何使用上述方法在查询中保留双斜杠?
(For例如:/test//test//?test=test//test〉/test/test/?test=test//test)
zyfwsgd66#
domain.com//test//test/?test/test 〉网站domain.com/test/test/