获取查询字符串中的变量 re_writes

发布于 2024-09-16 12:15:27 字数 368 浏览 3 评论 0原文

我试图将这四个获取变量(sch、val、lmt、ord)组合成一个漂亮的 url re_write。目前,我的 .htaccess 文件中有以下内容,如果包含所有变量,它就可以工作,但是,如果我只输入前两个变量(sch 和 val),它就会中断:

RewriteRule ^search/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ 
             search.php?sch=$1&val=$2&lmt=$3&ord=$4 [L]

www.domain.com/search/City/London/ 5/上升工程 www.domain.com/search/City/London 但这不是

I am trying to combine these four get variables (sch, val, lmt, ord) into a nice looking url re_write. Currently I have the following in my .htaccess file and it works if all variables are included, however, if I only input the first two variables (sch & val) it breaks:

RewriteRule ^search/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ 
             search.php?sch=$1&val=$2&lmt=$3&ord=$4 [L]

www.domain.com/search/City/London/5/asc works
www.domain.com/search/City/London but this doesn't

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

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

发布评论

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

评论(2

街道布景 2024-09-23 12:15:28

您可以使用它来获取整个路径作为文本并解析
www.domain.com/search/City/London/5/asc 工作 www.domain.com/search/City/London 但这不

RewriteRule ^search/(.*)$ search.php?url=$1
$paths=explode('/',$_GET('url'));

输出
www.domain.com/search/City/London/5/asc

$paths=city,london,5,asc

www.domain.com/search/City/London

$paths=city,london

您可以使用循环和配对作为密钥/值...如果需要的话

you can use this , to grab whole path as text and parse
www.domain.com/search/City/London/5/asc works www.domain.com/search/City/London but this doesn't

RewriteRule ^search/(.*)$ search.php?url=$1
$paths=explode('/',$_GET('url'));

output
www.domain.com/search/City/London/5/asc

$paths=city,london,5,asc

www.domain.com/search/City/London

$paths=city,london

You can use loop and pair as key/value... if needed

眼泪也成诗 2024-09-23 12:15:27

为什么不在其前面添加其他重写规则呢? IE:

 RewriteRule ^search/([^/]*)$ 
         search.php?sch=$1 [L]
 RewriteRule ^search/([^/]*)/([^/]*)$ 
         search.php?sch=$1&val=$2 [L]
 RewriteRule ^search/([^/]*)/([^/]*)/([0-9]*)$ 
         search.php?sch=$1&val=$2&lmt=$3 [L]
 RewriteRule ^search/([^/]*)/([^/]*)/([0-9]*)/([^/]*)$ 
         search.php?sch=$1&val=$2&lmt=$3&ord=$4 [L]

那应该可行。不确定这是否是您想要的,但我真的认为如果不执行查询并将完整的查询字符串作为一行传递然后在 PHP 端解析它,就“可能”做到这一点。

编辑

与 PHP 相比,在 .htaccess 中执行所有这些操作的好处是它需要更少的逻辑,并且您可以将所需的内容以正确的值传递给脚本。我的偏好是使用上面的 .htaccess。

Why not just add the other rewrite rules ahead of it. IE:

 RewriteRule ^search/([^/]*)$ 
         search.php?sch=$1 [L]
 RewriteRule ^search/([^/]*)/([^/]*)$ 
         search.php?sch=$1&val=$2 [L]
 RewriteRule ^search/([^/]*)/([^/]*)/([0-9]*)$ 
         search.php?sch=$1&val=$2&lmt=$3 [L]
 RewriteRule ^search/([^/]*)/([^/]*)/([0-9]*)/([^/]*)$ 
         search.php?sch=$1&val=$2&lmt=$3&ord=$4 [L]

That should work. Not sure if it is what you want, but I do not really think it is "possible" to do that without just doing a query and passing the full query string as one line then parsing it PHP side.

EDIT

The nice thing about doing all this in .htaccess vs PHP is it requires less logic and you pass what you want to your script in the correct values. My preference would be using the .htaccess above.

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