基于查询字符串的重定向
Redirection Based on Query String
不想让包含 300 个条目的 .htaccess 膨胀,我可以使用什么 javascript 根据对这个单个文件的请求中的查询字符串重定向到 URL。例如,
https://www.mywebsite.com/redirect.jhtml?Type=Cool&LinkID=57
我唯一关心的部分是 57 然后将它重定向到任何地方:
https://www.anothercoolwebsite/secretworld/
在以下情况下,取 34 并重定向:
https://www.mywebsite.com/redirect.jhtml?Type=Cool&LinkID=34
https://www.anoldwebsite.com/cool/file.html
谢谢!
这应该没问题。请记住,像 PHP 脚本这样的 server-side 解决方案将适用于更多客户。既然你提到了 .htaccess
,我想我应该让你知道 fallback resource command
无论如何,这是唯一的 JS 解决方案
function parseString(){//Parse query string
var queryString=location.search.substring(1);//Remove ? mark
var pair = queryString.split('&'); //Key value pairs
var returnVal={};
pair.forEach(function(item,i){
var currPair = item.split('=');//Give name and value
returnVal[currPair[0]]=currPair[1];
});
return returnVal;
}
var links=["index", "about"];//Sample array of links, make sure this matches up with your LinkID
location.href=links[parseString().LinkID]+".html"; //Redirect based on LinkID
不想让包含 300 个条目的 .htaccess 膨胀,我可以使用什么 javascript 根据对这个单个文件的请求中的查询字符串重定向到 URL。例如,
https://www.mywebsite.com/redirect.jhtml?Type=Cool&LinkID=57
我唯一关心的部分是 57 然后将它重定向到任何地方: https://www.anothercoolwebsite/secretworld/
在以下情况下,取 34 并重定向:
https://www.mywebsite.com/redirect.jhtml?Type=Cool&LinkID=34 https://www.anoldwebsite.com/cool/file.html
谢谢!
这应该没问题。请记住,像 PHP 脚本这样的 server-side 解决方案将适用于更多客户。既然你提到了 .htaccess
,我想我应该让你知道 fallback resource command
无论如何,这是唯一的 JS 解决方案
function parseString(){//Parse query string
var queryString=location.search.substring(1);//Remove ? mark
var pair = queryString.split('&'); //Key value pairs
var returnVal={};
pair.forEach(function(item,i){
var currPair = item.split('=');//Give name and value
returnVal[currPair[0]]=currPair[1];
});
return returnVal;
}
var links=["index", "about"];//Sample array of links, make sure this matches up with your LinkID
location.href=links[parseString().LinkID]+".html"; //Redirect based on LinkID