Is it possible to remove the # symbol from angular.js URLs?
I still want to be able to use the browser's back button, etc, when I change the view and will update the URL with params, but I don't want the # symbol.
The tutorial routeProvider is declared as follows:
angular.module('phonecat', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/phones', {templateUrl: 'partials/phone-list.html', controller: PhoneListCtrl}).
when('/phones/:phoneId', {templateUrl: 'partials/phone-detail.html', controller: PhoneDetailCtrl}).
otherwise({redirectTo: '/phones'});
}]);
Can I edit this to have the same functionality without the #?
Just add
$locationProvider
Inand then add
$locationProvider.hashPrefix('');
afterThat's it.
This answer assumes that you are using nginx as reverse proxy and you already did set $locationProvider.html5mode to true.
->For the people who might still be struggling with all cool stuff above.
Surely, @maxim grach solution works fine, but for the solution for how to respond to requests that doesn't want the hashtag to be included in the first place what can be done is check if php is sending 404 and then rewrite the url. Following is the code for nginx,
In the php location, detect 404 php error and redirect to another location,
Then rewrite url in redirect location and proxy_pass the data, offcourse, put your website url at the place of my blog's url. (Request : Question this only after you tried it)
And see the magic.
And it definitely works, atleast for me.
Start from index.html remove all
#
from<a href="#/aboutus">About Us</a>
so it must look like<a href="/aboutus">About Us</a>
.Now in head tag of index.html write<base href="/">
just after last meta tag.Now in your routing js inject
$locationProvider
and write$locatonProvider.html5Mode(true);
Something Like This:-For more Details watch this video https://www.youtube.com/watch?v=XsRugDQaGOo
I write out a rule in web.config after
$locationProvider.html5Mode(true)
is set inapp.js
.Hope, helps someone out.
In my index.html I added this to
<head>
Don't forget to install url rewriter for iis on server.
Also if you use Web Api and IIS, this match url will not work out, as it will change your api calls. So add third input(third line of condition) and give out a pattern that will exclude calls from
www.yourdomain.com/api
Follow 2 steps-
1. First set the $locationProvider.html5Mode(true) in your app config file.
For eg -
angular.module('test', ['ui.router']) .config(function($stateProvider, $urlRouterProvider, $locationProvider) { $locationProvider.html5Mode(true); $urlRouterProvider.otherwise('/'); });
2.Second set the <base> inside your main page.
For eg ->
<base href="/">
The $location service will automatically fallback to the hash-part method for browsers that do not support the HTML5 History API.
If you are in .NET stack with MVC with AngularJS, this is what you have to do to remove the '#' from url:
Set up your base href in your _Layout page:
<head> <base href="/"> </head>
Then, add following in your angular app config :
$locationProvider.html5Mode(true)
Above will remove '#' from url but page refresh won't work e.g. if you are in "yoursite.com/about" page refreash will give you a 404. This is because MVC does not know about angular routing and by MVC pattern it will look for a MVC page for 'about' which does not exists in MVC routing path. Workaround for this is to send all MVC page request to a single MVC view and you can do that by adding a route that catches all
url: