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.