htaccess 问题和一般正则表达式
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在你的情况下,我想我会使用这个:
但通常我更喜欢
然后
在代码中用
php
解析所有网址,你会犯一些错误,例如[^/\]+
....通常你必须像我上面提到的那样用后斜杠来转义正斜杠,在这种情况下
[^\/.]+
你不需要点,因为 < code>[^\/] 表达式匹配除正斜杠之外的任何内容你不需要
/?
(可选的正斜杠),因为它们之间的文本不是可选的,你可能会得到整个 url 作为$1
....也许只是最后一个很有用in your case I think I would use this:
but usually I prefer
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 slashyou 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