htaccess 问题和一般正则表达式

发布于 2024-11-10 16:22:54 字数 1400 浏览 0 评论 0原文

我的 htaccess 重写遇到问题。

该行是:

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

对于此网址: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 not found,我必须修改正则表达式并添加一个“.”所以:

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

那么就可以正常工作了。但如果我保留点,并检查旧网址,仅包含 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 个字母,救命! 谢谢:)


在我在两种情况下添加了两行后它工作得很好:

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

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

i'm having problems with my htaccess rewrites.

the line is :

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

for this url : http://localhost/host/sports/other/ -> it will result:

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

so far for this url, its all fine BUT for this URL:
http://localhost/host/sports/baseball/aliasss/
it will results : 404 not found, i have to modify the regex and add a '.' so:

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

it works fine then. but if i keep the dot, and check the old url, with 2 folders only, it will results this :

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

if you see the link is : /sports/basebal/l instead of /sports/baseball, 1 letter is missing from the query, HELP!
thanks :)


it worked fine after i added two lines n two cases :

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

but i dnt think this is the right way, that means if i want another level i have to add another line ...humph

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千鲤 2024-11-17 16:22:54

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

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 解析所有网址,你会犯一些错误,例如 [^/\]+ ....通常你必须像我上面提到的那样用后斜杠来转义正斜杠,

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

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

in your case I think I would use this:

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

but usually I prefer

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

and then parsing all the url with php

in your code you make few mistakes like [^/\]+ .... usually you have to escape the forward slashes with backward ones like I've mentioned above,

in this case [^\/.]+ you don't need the dot, because the [^\/] expression matches anything except the forward slash

you don't need /? (optional forward slashes) because the text between them is not optional and you will probably get the whole url as $1 .... maybe just the last one is useful

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文