首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >专门处理一个查询参数

专门处理一个查询参数
EN

Stack Overflow用户
提问于 2021-05-07 10:41:31
回答 2查看 60关注 0票数 4

我需要专门处理一个查询参数。url可以如下所示:

代码语言:javascript
复制
https://example.com/?app=egov&map=1337
https://example.com/?app=egov&map=1337&showoptions=true
https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorld

重定向应该指向

代码语言:javascript
复制
https://example.com/contextName/resources/apps/egov/index.html?map=1337
https://example.com/contextName/resources/apps/egov/index.html?map=1337&showoptions=true
https://example.com/contextName/resources/apps/egov/index.html?map=1337&anotherparam=helloWorld

如您所见,需要将app-Parameter用作URL--Parameter;其他-Parameter则需要像传统的查询-params一样使用。

我已经知道了如何实现第一部分,其中https://example.com/?app=egov使用以下方法指向https://example.com/contextName/resources/apps/egov/index.html

代码语言:javascript
复制
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^app\=(.+)
RewriteRule ^/$ "/contextName/resources/apps/%1/index.html" [R,L,QSA]

但我在纠结于如何正确处理其他的对撞机。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-05-07 17:11:03

所有这些查询字符串组合都可以由一个规则来处理。

代码语言:javascript
复制
RewriteEngine On

RewriteCond %{QUERY_STRING} ^app=([\w-]+)(?:&(.*))?$ [NC]
RewriteRule ^$ contextName/resources/apps/%1/index.html?%2 [L]

RewriteCond匹配app查询参数并在%1中捕获它,而在&%2中捕获之后,查询字符串的其余部分将被捕获。

票数 3
EN

Stack Overflow用户

发布于 2021-05-07 13:11:44

在显示的示例中,请尝试遵循htaccess规则文件。在测试您的URL之前,请确保清除浏览器缓存。

代码语言:javascript
复制
RewriteEngine ON
####Apache documentation link https://httpd.apache.org/docs/trunk/rewrite/remapping.html for more info.
##Internal rewrite rule for URL https://example.com/?app=egov&map=1337
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2 [L]

##Internal rewrite rule for URL https://example.com/?app=egov&map=1337&showoptions=true
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=(.*)$ [NC]
RewriteRule ^/?$  contextName/resources/apps/%1/index.html?map=%2&showoptions=%3 [L]

##Internal rewrite rule for https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorl
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=([^&]*)&anotherparam=(.*)$ [NC]
RewriteRule ^/?$  contextName/resources/apps/%1/index.html?map=%2&anotherparam=%4 [L]

您所有的URL uri部件都是空的,因此规则是基于此编写的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67433438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档