我需要专门处理一个查询参数。url可以如下所示:
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重定向应该指向
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
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^app\=(.+)
RewriteRule ^/$ "/contextName/resources/apps/%1/index.html" [R,L,QSA]但我在纠结于如何正确处理其他的对撞机。
发布于 2021-05-07 17:11:03
所有这些查询字符串组合都可以由一个规则来处理。
RewriteEngine On
RewriteCond %{QUERY_STRING} ^app=([\w-]+)(?:&(.*))?$ [NC]
RewriteRule ^$ contextName/resources/apps/%1/index.html?%2 [L]RewriteCond匹配app查询参数并在%1中捕获它,而在&在%2中捕获之后,查询字符串的其余部分将被捕获。
发布于 2021-05-07 13:11:44
在显示的示例中,请尝试遵循htaccess规则文件。在测试您的URL之前,请确保清除浏览器缓存。
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部件都是空的,因此规则是基于此编写的。
https://stackoverflow.com/questions/67433438
复制相似问题