Redirect from Hash to HashBang using angular

…衆ロ難τιáo~ 提交于 2020-01-06 04:31:19

问题


I have published a website using mvc c# with angular and AJAX calls to create some client side pages. One of my links look like this:

www.website.com/App#/Index

I am trying to setup a hasbang solution to make my site crawlable. I succesfully created the hashbang setup so now a working link will look like this:

www.website.com/App#!/Index

The issue now is that all my links out on Facebook and so on does not contain the bang (!) and therefore those links are dead. How do I redirect from the first link without hashbang to the link with hashbang?

My current routing:

app.config(['$routeProvider', '$locationProvider',
function ($routeProvider, $locationProvider) {
    $locationProvider.html5Mode(false).hashPrefix('!');
    $routeProvider
      .when('/Index', {
          templateUrl: '/Apps/Modules/IndexPublicAngular/Index.html', controller: 'IndexPublicController'
      });
}]);

回答1:


I hit a similar issue that you had where I need to use hashbang instead of just hash. I had to come up with a quick and dirty solution for this:

$rootScope.$watch(function () {
    return window.location.hash;
}, function (value) {
    if (value.indexOf('#!#') == 0) {
      setTimeout(function() {
        window.location.hash = value.replace('#!#','#!');
      })
    }
});

There is probably a better solution out there, but I didn't investigate very deeply since this is only a temporary patch for bookmarked urls etc. Keep in mind since this only fires after angular has already touched the url, '#' is already converted into '#!#' by angular.



来源:https://stackoverflow.com/questions/32626081/redirect-from-hash-to-hashbang-using-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!