htaccess问题和一般正则表达式


htaccess problems and general regex

我的htaccess重写有问题。

线路是:

RewriteRule ^sports/([^/']+)/?([^/']+)/?([^/'.]+)/?$ index.php?v=sports&title=$1&go=$2&d=$3 [L]

对于这个url:http://localhost/host/sports/other/->它将产生:

***object(url)[3]
  public 'v' => string 'sports' (length=6)
  public 'title' => string 'other' (length=5)*** ( these are the var_dump of Gets);

到目前为止,对于这个url,一切都很好,但对于这个url:http://localhost/host/sports/baseball/aliasss/结果是:404找不到,我必须修改正则表达式并添加一个"."所以:

RewriteRule ^sports/([^/']+)/?([^/'.]+)/?([^/'.]+)/?$ index.php?v=sports&title=$1&go=$2&d=$3 [L]

那就行了。但如果我保留这个点,并检查旧的url,只有2个文件夹,它会得到这样的结果:

object(url)[3]
  public 'v' => string 'sports' (length=6)
  public 'title' => string 'basebal' (length=7)
  public 'go' => string 'l' (length=1)

如果您看到链接是:/sports/basebal/l而不是/sports/baseball,则查询中缺少1个字母,HELP!谢谢:)


在我添加了两行两种情况后,它运行良好:

RewriteRule ^sports/([^/']+)/?([^/'.]+)/?$ index.php?v=sports&title=$1 [L]
RewriteRule ^sports/([^/']+)/?([^/'.]+)/?([^/'.]+)/?$ index.php?v=sports&title=$1&go=$2&d=$3 [L]

但我不认为这是正确的方式,这意味着如果我想要另一个级别,我必须添加另一行。。。驼峰

在您的情况下,我想我会使用这个:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^sport/(.*?)/(.*?)/(.*?)/$ test.php?v=sport&a=$1&b=$2&c=$3

但通常我更喜欢

RewriteRule (.*) index.php?get=$1

然后用php 解析所有url

在您的代码中,您很少会犯[^/']+这样的错误。。。。通常你必须像我上面提到的那样,用后斜线来躲避前斜线,

在这种情况下,[^'/.]+不需要点,因为[^'/]表达式匹配除正斜杠之外的任何内容

你不需要/?(可选的正斜杠),因为它们之间的文本不是可选的,你可能会得到整个url作为$1。。。。也许最后一个是有用的