当用户转到基本域http://www.domain.com/或http://domain.com/时,
该页面必须重定向到http://www.domain.com/#!/news.html
我的意思是这样的:
if (window.location.href("http://www.domain.com/") || window.location.href("http://domain.com/")) {
window.location.replace("http://domain.com/#!/news.html");
}
这行不通。特别是,该页面在两个不同的域上提供。
任何想法如何用漂亮的Javascript或jQuery(使用jQuery 1.4.2)解决它?
最佳答案
在location
上使用解析的URL属性,例如pathname
,hostname
,search
和hash
,而不要弄乱href
:
if (location.pathname==='/' && location.hash==='')
location.hash= '#!/news.html';
特别是,该页面在两个不同的域上提供。
对于SEO来说,最好将您的网站仅放在一个特定的主机名上,然后将任何其他关联的主机名重定向到该规范的主机名。让HTTPD配置担心域而不是您的应用程序。例如,如果您选择
www.domain.com
作为您的规范主机名,则在Apache配置中您可以说:<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>
其他Web服务器具有不同的设置重定向的方式。如果您无权访问Apache配置,并且只能使用
.htaccess
(ugh),则必须使用mod_rewrite
根据主机名进行条件重定向。关于javascript - 如何正确处理javascript重定向?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3797551/