gpt4 book ai didi

php - 在 htaccess/php 中传递动态变量

转载 作者:可可西里 更新时间:2023-11-01 12:30:56 26 4
gpt4 key购买 nike

我创建了一个动态站点,信息(区域/类别/子类别)通过 htaccess 传递以获得漂亮的 URL。例如,当用户深入到类别时,它会按如下方式流动:

/parts/hard-drives

/parts/hard-drives/hard-drives-sas

我想知道是否有一种方法可以在 htaccess 中扩展它,这样我就可以传递更多项目并使用 php 获取变量,即:

/parts/hard-drives/hard-drives-sas/?manufacturer=dell&price=20

我当前的 htaccess 文件行如下:

RewriteRule ^parts/([^/\.]+)/([^/\.]+)/?$ parts.php?p=$1&f=$2 [L]
RewriteRule ^parts/([^/\.]+)/?$ parts.php?p=$1 [L]
RewriteRule ^parts parts.php [L]

您会看到第一行与 /products/hard-drives/hard-drives-sas 相关,第二行与 /products/hard-drives 相关,然后就是零件页面 /parts

希望这是有道理的!

谢谢。

最佳答案

使用QSA标志传递GET变量:

RewriteRule ^parts/([^/\.]+)/([^/\.]+)/?$ parts.php?p=$1&f=$2 [L,QSA]
RewriteRule ^parts/([^/\.]+)/?$ parts.php?p=$1 [L,QSA]
RewriteRule ^parts parts.php [L,QSA]

文档:

When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.

Consider the following rule:

RewriteRule /pages/(.+) /page.php?page=$1 [QSA]

With the [QSA] flag, a request for /pages/123?one=two will be mapped to /page.php?page=123&one=two. Without the [QSA] flag, that same request will be mapped to /page.php?page=123 - that is, the existing query string will be discarded.

http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_qsa

关于php - 在 htaccess/php 中传递动态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14383258/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com