如果有人输入“www.morgancc.edu”,我希望它重定向到位于“www.morgancc.edu/m”的移动网站。但是,我只需要使用此确切的URL进行重定向。如果您要访问“www.morgancc.edu/programs”之类的东西,那么我不希望它重定向。这是我到目前为止的代码:

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

最佳答案

location .hostname带有空路径是您似乎想要的

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/';
}

您可能需要在此处或此处添加一些

08-15 16:04