I'm learning AngularJS and there's one thing that really annoys me.
I use $routeProvider
to declare routing rules for my application:
$routeProvider.when('/test', {
controller: TestCtrl,
templateUrl: 'views/test.html'
})
.otherwise({ redirectTo: '/test' });
but when I navigate to my app in browser I see app/#/test
instead of app/test
.
So my question is why AngularJS adds this hash #
to urls? Is there any possibility to avoid it?
In fact you need the # (hashtag) for non HTML5 browsers.
Otherwise they will just do an HTTP call to the server at the mentioned href. The # is an old browser shortcircuit which doesn't fire the request, which allows many js frameworks to build their own clientside rerouting on top of that.
You can use
$locationProvider.html5Mode(true)
to tell angular to use HTML5 strategy if available.Here the list of browser that support HTML5 strategy: http://caniuse.com/#feat=history
Lets write answer that looks simple and short
thanks To @plus- for detailing the above answer
You could also use the below code to redirect to the main page (home):
After specifying your redirect as above, you can redirect the other pages, for example:
In Angular 6, with your router you can use:
If you enabled html5mode as others have said, and create an
.htaccess
file with the following contents (adjust for your needs):Users will be directed to the your app when they enter a proper route, and your app will read the route and bring them to the correct "page" within it.
EDIT: Just make sure not to have any file or directory names conflict with your routes.
**
** Because