fel*_*589 1 .htaccess redirect
我尝试进行搜索并尝试了解如何使用(多个)查询字符串进行重定向,但我没有运气.我希望有人能帮我理解这个问题:)
我正在开发这个电子商务商店,人们正在搜索电子商务搜索输入,查找位于不同CMS中的内容.例如,单词"返回".这不是电子商务系统中的产品,因此当然会返回结果错误(未找到产品).
我的想法只是手动将这些quieres重定向到CMS中的正确登录页面.
以下是电子商务系统中"返回"网址的示例:
http://www.domain.com/catalog/search.php?mode=search&page=1&substring=return
Run Code Online (Sandbox Code Playgroud)
这是我想送人的地方:
http://www.domain.com/catalog/Returns.html
Run Code Online (Sandbox Code Playgroud)
有关如何做到这一点的任何想法?提前致谢!
菲尔建议,这样做的方法; 但有一些(小)修改:
RewriteEngine On
RewriteCond %{QUERY_STRING} substring=returns? [NC]
RewriteRule . /catalog/Returns.html? [L]
RewriteCond %{QUERY_STRING} substring=shipping [NC]
RewriteRule . /catalog/Shipping.html? [L]
Run Code Online (Sandbox Code Playgroud)
注意如果您只想删除一个参数,请参阅下面的附加信息和说明.
NB对于更加严格的匹配看哪里&成为一个问题之下.
我解释差异的最佳方式(在上面和菲尔的原创之间)和你遇到问题的原因是解释发生了什么......
RewriteCond %{QUERY_STRING} substring=returns? [NC]在这种情况下检查查询字符串中是否有正则表达式的实例substring=returns?*.
该[NC]标志仅表示匹配大写和小写字母.
*澄清:正则表达式(substring=returns?)表示substr=return字面匹配,有或没有s.
如果满足条件(即在查询字符串中匹配正则表达式模式),则触发重写规则.这就是问题所在......
给定URL:http://example.com/?substring = return
原规则:
RewriteRule . /catalog/Returns.html [L]
Run Code Online (Sandbox Code Playgroud)
重写URL,保留查询字符串,如下所示:
http://example.com/?substring=returns
http://example.com/catalog/Returns.html?substring=returns
http://example.com/catalog/Returns.html?substring=returns
http://example.com/catalog/Returns.html?substring=returns
http://example.com/catalog/Returns.html?substring=returns
...and so on until limit is reached...
Run Code Online (Sandbox Code Playgroud)
旁注:该[L]标志会阻止.htaccess文件执行任何其他规则,但它不会阻止它再次循环.
然后解决方案是覆盖查询字符串(因为我们不再需要它)你可以简单地通过添加?到以下结尾RewriteRule:
RewriteRule . /catalog/Returns.html? [L]
Run Code Online (Sandbox Code Playgroud)
注意如果您只想删除一个参数,请参阅下面的附加信息和说明.
NB对于更加严格的匹配看哪里&成为一个问题之下.
以下资源可能会对将来有所帮助:
http://httpd.apache.org/docs/current/rewrite/flags.html
http://www.regular-expressions.info/ - 查看教程部分
&成了问题RewriteCond %{QUERY_STRING} &substring=returns? [NC]
Run Code Online (Sandbox Code Playgroud)
在上面,正则表达式意味着匹配字符&substring=return与s附加的可选项.
所以它将按预期匹配以下内容:
http://example.com/?var1=somvalue&substring=return
http://example.com/?var1=somvalue&substring=returns
http://example.com/?var1=somvalue&substring=return&var2=othervalue
http://example.com/?var1=somvalue&substring=returns&var2=othervalue
Run Code Online (Sandbox Code Playgroud)
哪个很好,并且给定原始查询字符串不会有问题,但是,如果我导航到页面并以不同的顺序写入参数,则&不一定存在,因此它不匹配(什么时候应该):
http://example.com/?substring=return&var1=somevalue
http://example.com/?substring=returns&var1=somevalue
Run Code Online (Sandbox Code Playgroud)
简单地摆脱它(就像我做的那样)可以解决这个问题,但它不会带来风险.
RewriteCond %{QUERY_STRING} substring=returns? [NC]
Run Code Online (Sandbox Code Playgroud)
如果您要引入一个新参数secondsubstring,例如它应该匹配时不应该:
Good Match > http://example.com/?substring=return&var1=somevalue
Good Match > http://example.com/?var1=somevalue&substring=return
Bad Match > http://example.com/?secondsubstring=return&var1=somevalue
Run Code Online (Sandbox Code Playgroud)
要解决此潜在问题,您可以执行以下操作:
RewriteCond %{QUERY_STRING} ^(.*&)?substring=returns?
Run Code Online (Sandbox Code Playgroud)
以上将匹配:
http://example.com/?substring=return&var1=somevalue
http://example.com/?var1=somevalue&substring=return
Run Code Online (Sandbox Code Playgroud)
但不匹配:
http://example.com/?secondsubstring=return&var1=somevalue
Run Code Online (Sandbox Code Playgroud)
另一个潜在的问题是表达式匹配:
http://example.com/?substring=returning&var1=somevalue
http://example.com/?substring=return%20television&var1=somevalue
Run Code Online (Sandbox Code Playgroud)
我的理解是,在特定情况下这不会成为问题.但是如果它是一个问题你可以做:
RewriteCond %{QUERY_STRING} ^(.*&)?substring=returns?(&|$)
Run Code Online (Sandbox Code Playgroud)
上面检查return/ 后面的字符returns是&信号变量的结尾以及新变量的开始还是查询字符串的结尾.
在某些情况下,菲尔指出,最好一次只删除一个参数,并保持查询字符串的其余部分不变.
你可以很简单地通过实现捕获组并在以下位置RewriteCond输出它们来实现这一点RewriteRule:
RewriteCond %{QUERY_STRING} ^(.*&)?substring=returns?(&.*)?$ [NC]
RewriteRule . /catalog/Shipping.html?%1%2 [L]
Run Code Online (Sandbox Code Playgroud)
您可以使用%N从重写条件$N插入捕获组并从重写规则插入捕获组.
所以在这种情况下我们重定向到:
/catalog/shipping.html?(RewriteCond Group1)(RewriteCond Group2)
/catalog/Shipping.html?%1%2
Run Code Online (Sandbox Code Playgroud)
该[L]标志 - 如前所述 - 停止处理.htaccess文件中的任何规则
^(.*&)?substring=returns?(&.*)?$
Run Code Online (Sandbox Code Playgroud)
^ 字符串的开头(.*&)? 第一个捕获组
.0次或更多次*&?使得整个组可选substring=returns?substring=return字面上匹配可选s(&.*)? 第二个捕获组
&.0次或更多次*?又让组可选$ 字符串结束[L] 国旗vs [END]为了完整起见......
该[L]标志阻止文件.htaccess进一步向下移动任何规则.htaccess.
该[END]标志完全停止重写过程.
举例说明:
while(TRUE){
if(condition1){ continue; }
if(condition2){ continue; }
if(condition3){ continue; }
if(condition4){ continue; }
}
while(TRUE){
if(condition1){ break; }
if(condition2){ break; }
if(condition3){ break; }
if(condition4){ break; }
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码块中,该[L]标志就像一个continue语句,它跳过代码块的其余部分并再次启动.虽然[END]标志充当break声明并完全停止循环.
如果我们[L]用[END]菲尔的原始答案替换旗帜那么它就可以了.随着中提到的注意事项如果&成为一个问题部分上方.
| 归档时间: |
|
| 查看次数: |
3414 次 |
| 最近记录: |