7

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>

3 Answers 3

9

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

Alternatively look at the 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

Sign up to request clarification or add additional context in comments.

2 Comments

Makes sense for what I am doing. However, now it will not redirect at all.
Your second suggested worked! This is the code I used: '<script type="text/javascript"> if (window.location.href == "morgancc.edu" ) { window.location.href = 'morgancc.edu/m'; } </script>'
9

The equality operator is ==, not =.

Comments

6

I suggest on using a sever-side scripting language to redirect base on the device visiting your site. you also have a typo in your if statement, you should have == not =

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

1 Comment

Using this code in my blogger site. Works perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.