基于URL的重定向 - JavaScript [英] Redirection based on URL - JavaScript

查看:41
本文介绍了基于URL的重定向 - JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人输入'www.morgancc.edu',我希望它重定向到位于'www.morgancc.edu/m'的移动网站。但是,我只需要重定向 exact URL。如果你去www.morgancc.edu/programs之类的东西,我不希望它重定向,这是它目前正在做的事情。这是我到目前为止的代码:

If someone types in 'www.morgancc.edu', I want it to redirect to our mobile site located at 'www.morgancc.edu/m' However, I only need to redirect with this exact URL. I don't want it to redirect if you go to something like 'www.morgancc.edu/programs', which is what it is currently doing. Here is the code I have so far:

<script type="text/javascript">
if (window.location = "www.morgancc.edu") {
   window.location.href = 'http://www.morgancc.edu/m/'; 
}
</script>


推荐答案

位置。带有空路径的.hostname就是您想要的

location.hostname with an empty path is what you seem to want

if (window.location.hostname == "www.morgancc.edu" && 
    window.location.pathname=="" ) {
   window.location.href = 'http://www.morgancc.edu/m/'; 
}

或者看看href

if (window.location.href== "http://www.morgancc.edu") {
   window.location.href = 'http://www.morgancc.edu/m/'; 
}

您可能需要添加一些/此处或那里

You may need to add some / here or there

这篇关于基于URL的重定向 - JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆