【问题标题】:regex and getting slashes plus characters from a url正则表达式并从 url 获取斜杠和字符
【发布时间】:2012-01-31 12:26:29
【问题描述】:

为什么这条规则不返回带有字符的斜线?

RewriteRule ^test/(.+)/(.+)/(.+)/?$ test.php?one=$1&two=$2&three=$3

例如在这个网址中

mysite.com/test//one///two////three///

它会返回

$1='one' 

$2='two' 

$3='three' 

但我希望它返回

$1='/one' 

$2='//two' 

$3='///three///' 

我也试过^posts/{1}(.+)/{1}(.+)/{1}(.+)/?$,但也没有用...

我没有想到其他任何事情。任何人都可以帮忙吗?

提前致谢

【问题讨论】:

    标签: regex .htaccess mod-rewrite slash


    【解决方案1】:

    服务器之前可能已经处理过 URL,删除了双斜杠。如果匹配完整的 URL,所有的斜线是否仍然存在?

    【讨论】:

    • 谢谢!我为之前的 URL 尝试了这个正则表达式 ^test/(.*)$,输出为 $1='one/two/three/'。似乎 apache 删除了多个斜杠而不重定向到更正的 URL。我试图防止我的网站出现重复内容,但 apache 让它变得更加困难!
    【解决方案2】:

    试试这个:

    RewriteRule ^test(/+[^/]+)(/+[^/]+)(/+[^/]+/*)$ test.php?one=$1&two=$2&three=$3
    

    【讨论】:

    • 这个正则表达式运行良好,对于以前的 URL(apache 自动更改为 mysite.com/test/one/two/three/ 结果是 $1='/one' $2='/two' $3='/three/' 并且如果 apache 不删除多个斜杠似乎可以工作!
    猜你喜欢
    • 2018-07-31
    • 2015-07-14
    • 2011-03-09
    • 2023-02-07
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    相关资源
    最近更新 更多