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>
Terri Eades
  • 93
  • 1
  • 1
  • 4

3 Answers3

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

mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Makes sense for what I am doing. However, now it will not redirect at all. – Terri Eades Aug 02 '13 at 17:33
  • Your second suggested worked! This is the code I used: '' – Terri Eades Aug 02 '13 at 17:45
8

The equality operator is ==, not =.

L84
  • 45,514
  • 58
  • 177
  • 257
fred02138
  • 3,323
  • 1
  • 14
  • 17
5

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>