How do I get a traditional url structure in a single page application (NO hash-bang)?

喜夏-厌秋 提交于 2019-12-24 20:34:14

问题


I want to build a single page application with angular.js the only thing is i want a traditional url structure.

Example:

  Before: www.example.com/#/index  
  After:  www.example.com/index  

  Before: www.example.com/#/user/21345678  
  After:  www.example.com/user/21345678  

First of all is it possible?
Second how would i do that?
Any server except Apache XD


回答1:


You need to enable HTML5 mode within angular.

.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
  $locationProvider.html5Mode(true).hashPrefix('!');

Then you also need to configure your sever. See this article: Configure your server to work with html5Mode

I'd suggest reading this short post: Pretty URLs in Angular, Removing the Hashtag. In it they also recommend setting <base href="/"> within the head of your HTML.



来源:https://stackoverflow.com/questions/22873418/how-do-i-get-a-traditional-url-structure-in-a-single-page-application-no-hash-b

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