php - 如何在 .htaccess 中访问 query_string 中的键和值以在更改后的 url 中使用它们?
问题描述
我正在尝试使用获取参数转换 url
localhost:8080/goto/?fruits=1&apples=2
进入
localhost:8080/fruits/1/apples/2
使用以下命令:
RewriteEngine On
RewriteCond %{QUERY_STRING} !apples [NC]
我可以验证访问的 url 是否具有键 apples、fruits 或附加的任何 get 参数,以区分具有不同键的多个 url,例如 berrys=1 或 melons=7 等。
使用以下命令
RewriteRule ^ fruits/([^/]*)/apples/([^/]*)$ goto/index.php?fruits=$1&apples=$2 [L]
我可以将 url localhost:8080/fruits/1/apples/2 更改为 localhost:8080/fruits/1/apples/2
如何优化 RewriteCond 中的命令以在 RewriteRule 中使用多个键和值作为 $1、$2 等而不对多行进行硬编码?不像:
RewriteEngine On
RewriteCond %{QUERY_STRING} !apples [NC]
RewriteRule ^fruits/([^/]*)/apples/([^/]*)$ goto/index.php?fruits=$1&apples=$2 [L]
RewriteCond %{QUERY_STRING} !melons [NC]
RewriteRule ^fruits/([^/]*)/melons/([^/]*)$ goto/index.php?fruits=$1&melons=$3 [L]
RewriteCond %{QUERY_STRING} !berrys [NC]
RewriteRule ^fruits/([^/]*)/berrys/([^/]*)$ goto/index.php?fruits=$1&berrys=$3 [L] ....
解决方案
推荐阅读
- javascript - 无法从反应上传图像以使用 Multer 表达 api
- c# - 当鼠标悬停在统一的游戏对象上时,鼠标悬停不会改变材质
- python - Django PasswordResetView - 在哪里可以找到实际发送电子邮件的代码?
- sql - 如何对 COUNT 值求和
- javascript - 如何正确地将在 React 中以状态传递的 Date 对象放入 datepicker?
- java - SolrHealthIndicator 没有弃用 CompositeHealthIndicator
- javascript - 我有一个很长的 json,我希望在页面加载时只显示四个,并在用户单击投资链接时显示其余部分
- oracle - Oracle (XE) & SQL-Developer:如何找出正在使用的时间/日期/时间戳格式?
- c# - 最后用空值排序
- lotus-notes - Lotusscript - 使用 ArrayGetIndex 时类型不匹配