views:

48

answers:

2

Hi all,

When a user goes to the base domain http://www.domain.com/ or http://domain.com/,
the page has to be redirected to http://www.domain.com/#!/news.html

I mean something like this:

if (window.location.href("http://www.domain.com/") || window.location.href("http://domain.com/")) {
    window.location.replace("http://domain.com/#!/news.html");
}

This won't work. Especially, the page is provided on two different domains.
any ideas how to solve it in pretty Javascript or jQuery (using jQuery 1.4.2)?

+4  A: 
var loc =  window.location.href;

if(loc == 'http://domain.com' || loc == 'http://www.domain.com') {
  window.location.href = 'http://domain.com/#!/news.html';
}

Edit :

var d   = window.location.hostname;
var loc = window.location.href;
if(loc == d) { 
  window.location.href = d +'/#!/news.html';
}

document.domain really saved my life to answer your question, I tried to answer your question using regular expression, my patterns failed to match the available possibilities. seems like I really need to get back to learn regex :)

Edit :

var d = window.location.hostname;
Ninja Dude
this works, thanks. this website has two domain names, domain.com and domain1.com. Now I would do this snippet twice, for both domains. Or is there a better way to do that?
Thomasz
@thomasz I've updated my answer, give it a try =)
Ninja Dude
@Avinash thanks for your effort:) It seems to be a pretty nice solution, but unfortunately 'document.domain' wont work. Address won't change and the server responded with a status of 404. The snippet is fired before any js is loaded, is there to respect more things in use with 'document.domain'?
Thomasz
@thomasz then you need to match the domain with a regular expression, I tried to do it using a RegEx and pull out the domain name to match with `window.location.href` which I told you before. I guess regex is the right (and only) solution to ur problem
Ninja Dude
Ninja Dude
@Avinash with window.location.hostname it's the same issue like document.domain. I guess I have to try the RegEx solution you said...
Thomasz
+2  A: 

Use the parsed-URL properties on location, like pathname, hostname, search and hash rather than trying to mess with the href:

if (location.pathname==='/' && location.hash==='')
    location.hash= '#!/news.html';

Especially, the page is provided on two different domains.

It's best for SEO to put your site on only one particular hostname, and redirect any other associated hostnames to that canonical hostname. Let the HTTPD config worry about domains instead of your app. For example if you chose www.domain.com to be your canonical hostname, in Apache config you could say:

<VirtualHost *:80>
    ServerName www.domain.com
    DocumentRoot ...
    ... other settings for the main site ...
</VirtualHost>

<VirtualHost *:80>
    ServerName domain.com
    ServerAlias someotherdomain.org
    ServerAlias www.someotherdomain.org
    Redirect permanent / http://www.domain.com/
</VirtualHost>

Other web servers have a different ways of setting up redirects. If you have no access to the Apache config and you're limited to .htaccess (ugh) then you'd have to use mod_rewrite to do a conditional redirect based on the hostname.

bobince
@bobince thanks, your solution works! I take notice about the server solution, looking forward to change that.
Thomasz