开发者

htaccess problems and general regex

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]
  p开发者_JS百科ublic '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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜