angular重写url设置

因为hash目前还不被搜索引擎认可,如果需要页面被搜索引擎抓取得开启html5mode
$locationProvider.html5Mode(true);开启html5mode
如果未开启html5mode
a 标签的 href 的链接需要添加#号来跳转到其他页面
在没开启html5mode的状态下angular是用#hash来进行路由分配的
如果开启html5mode
需要在服务器端对重写url进行入口页面返回
需要在index头部添加

设定相应的基础连接
例 index.html 访问路径如下

http://www.xxx.com/base/index.html

base设定为”/base/”
之后页面内的连接就可以采用相对路径的连接了
开启html5mode默认会重写链接
例 index.html

http://www.xxx.com/base/index.html

会被重写为

http://www.xxx.com/base/

a标签的 href 的链接

就会访问

http://www.xxx.com/base/list

如果需要访问

http://www.xxx.com/base1


这样就可以做到不同页面之间的切换

如果业务需求需要不保存浏览历史记录可以使用
function replace(url){
if(!url) return;
try{
window.history.replaceState(null,null,’#’+url);
}catch(E){
$location.path(url);
}
}

Categories: Html5/CSS3, javaScript, 前端开发