Navigating to url directly after removing hashbangs not working

时光毁灭记忆、已成空白 提交于 2019-12-08 04:55:24

问题


I am using angular for my app

I wanted to remove the # from the url so i added the below lines as suggested in SO answer

$locationProvider.html5Mode({
  enabled: true,
  requireBase: false
});

In the index.html I also added the below code as suggested here

<head>
  <base href="/">
</head>

It all works fine when I navigate to pages from the home, but when i copy the url and open it in new tab, it throws 404 error

Example

When I launch the app, it's opening http://localhost:portno/home. When I refresh the page, I'm getting a 404 error.

What other configuration should i make?

My code structure is as below

  .state('tab.home', {
            url: '/home',
            views: {
                'tab-home': {
                    templateUrl: 'templates/tab-home.html',
                    controller: 'templeHome'
                }
            }
        })
        .state('tab.list', {
            url: '/list',
            views: {
                'tab-home': {
                    templateUrl: 'templates/list.html',
                    controller: 'templeList'
                }
            }
        })

回答1:


You need to add a route on your server that will redirect you to the entrypoint of your front (i.e: index.html).

For example, if you were redirected from your home to http://localhost:portno/foo/bar, you'll need a route to match the /foo/bar one that will redirect you to your index.html.

It migth look like this (note that this is an example code of my own written for Hapi):

server.route([
    {
        method: 'GET',
        path: '/foo/bar',
        handler: function(request, reply) {
            reply.file('./public/index.html');
        }
    }
    ...


来源:https://stackoverflow.com/questions/31981730/navigating-to-url-directly-after-removing-hashbangs-not-working

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