admin管理员组文章数量:1313925
Can't use htaccess, so have to rely on javascript.
Say the url is example/page1/page2/page3/ and I want to redirect this to example/page4/page3/ how would I do this? (and have this work for anything that has anything as page3 and has any subfolders after that as well). And also that page3 can be anything, so a wildcard would have to be in place for this..
Plus: Say the url is example/page1/page2/page3/page5/183/ and I want to redirect this to example/page4/page3/ how would I do this? (cutting off page5 plus anything on the url after that).
Can't use htaccess, so have to rely on javascript.
Say the url is example/page1/page2/page3/ and I want to redirect this to example/page4/page3/ how would I do this? (and have this work for anything that has anything as page3 and has any subfolders after that as well). And also that page3 can be anything, so a wildcard would have to be in place for this..
Plus: Say the url is example/page1/page2/page3/page5/183/ and I want to redirect this to example/page4/page3/ how would I do this? (cutting off page5 plus anything on the url after that).
Share Improve this question edited Jan 30 at 20:47 Malachi asked Jan 30 at 17:48 MalachiMalachi 9776 gold badges16 silver badges32 bronze badges 2 |2 Answers
Reset to default 0Test the contents of the current URL's pathname
; if it startsWith("/page1/page2/page3")
then do the redirect:
const currentPath = new URL(window.location.href).pathname;
if (currentPath.startsWith("/page1/page2/page3"))
window.location.replace("/page4/page3");
var pathArray = window.location.pathname.split('/');
var LvlLoc1 = pathArray[1];
var LvlLoc2 = pathArray[2];
var LvlLoc3 = pathArray[3];
var LvlLoc4 = pathArray[4];
then do if statements to verify if LvlLoc1-4 are anything and then do a location.href="https://example/" + whatever you want it to be. As in my example the url is example/page1/page2/page3/ and I want to redirect this to example/page4/page3/:
location.href = "https://example/" + LvlLoc4 + "/" + LvlLoc3 + "/"
I hope that helps anyone out.
本文标签: Wildcard javascript url redirectStack Overflow
版权声明:本文标题:Wildcard javascript url redirect - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741946636a2406455.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
rely on javascript
If you mean javascript running in a browser you would need to make the urls valid before using them. If you mean javascript running on the server it should be pretty easy. Please clarify. – James Commented Jan 30 at 17:59