[英]Rewrite part of query string with apache mod_rewrite
Need help with rewriting part of query string with mod_rewrite. 在使用mod_rewrite重写部分查询字符串时需要帮助。
Looked through a lot of resources and have a general understanding of how stuff works but cannot figure out correct solution 浏览了大量资源,对东西的工作原理有一个总体了解,但无法找出正确的解决方案
This link: 该链接:
http://example.com/?param=home&shop_id=1000005620&ate=bow&b_uid=-1&tg=one
Has to become this 必须成为这个
http://example.com/?param=home/#/shop/1000005620?ate=bow&b_uid=-1&tg=one
If shorter, then this part of query string 如果更短,则这部分查询字符串
&shop_id=1000005620&
transforms into /#/shop/1000005620?
&shop_id=1000005620&
转换为/#/shop/1000005620?
UPDATE: 更新:
Answer gave me a clear understanding what I had to do. 回答使我清楚地了解了我必须做什么。 Exact rules that fixed my issue were like this:
解决我的问题的确切规则是这样的:
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} ^(.*)&shop_id=([0-9]{10,12})(?:&)(.*)$
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1?%1/#/shop/%2\?%3 [NE,L,R]
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} ^shop_id=([0-9]{10,12})$
RewriteRule ^(.*)$ https://%{SERVER_NAME}/#/shop/%1? [NE,L,R]
</IfModule>
Reason for this rewrite rules is hash handling with safari and ie I have links that are used by external pages and also I have a redirection to https if site request http. 该重写规则的原因是使用safari进行哈希处理,即,我具有外部页面使用的链接,并且如果站点请求http,也可以重定向到https。 If there is a hash in the link and request is sent from Safari or IE hash goes away from the URL and does not come back after redirection.
如果链接中有哈希,并且从Safari发送了请求,则IE哈希将离开URL,并且在重定向后不会返回。 I want to note very important fact that Chrome, Firefox do not have problems with keeping the URL with hash even after redirect to https.
我想指出一个非常重要的事实,即使在重定向到https之后,Chrome和Firefox在保留带有哈希值的URL方面也没有问题。 This involved to reconstruct our URL but it is worth it and now everything is working as it should.
这涉及到重建我们的URL,但这是值得的,现在一切都按预期进行。
You can try: 你可以试试:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)&shop_id=([^&]+)&?(.*)$
RewriteRule ^(.*)$ /$1?%1/#shop_id=%2?%3 [L,R,NE]
EDIT: 编辑:
what about if I only what to rewrite
http://example.com/?shop_id=1000005620
intohttp://example.com/#/shop/1000005620
what the rewrite rule be then?如果我只将
http://example.com/?shop_id=1000005620
重写为http://example.com/#/shop/1000005620
,该怎么办?
Just change the relevant parts of the regex pattern: 只需更改正则表达式模式的相关部分:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^shop_id=([^&]+)$
RewriteRule ^(.*)$ /$1/#shop_id=%2? [L,R,NE]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.