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 th
Just add $locationProvider
In
.config(function ($routeProvider,$locationProvider)
and then add $locationProvider.hashPrefix('');
after
.otherwise({
redirectTo: '/'
});
That's it.
According to the documentation. You can use:
$locationProvider.html5Mode(true).hashPrefix('!');
NB: If your browser does not support to HTML 5. Dont worry :D it have fallback to hashbang mode. So, you don't need to check with
if(window.history && window.history.pushState){ ... }
manually
For example: If you click: <a href="/other">Some URL</a>
In HTML5 Browser:
angular will automatically redirect to example.com/other
In Not HTML5 Browser:
angular will automatically redirect to example.com/#!/other
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.
I write out a rule in web.config after $locationProvider.html5Mode(true)
is set in app.js
.
Hope, helps someone out.
<system.webServer>
<rewrite>
<rules>
<rule name="AngularJS" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
In my index.html I added this to <head>
<base href="/">
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
You can tweak the html5mode but that is only functional for links included in html anchors of your page and how the url looks like in the browser address bar. Attempting to request a subpage without the hashtag (with or without html5mode) from anywhere outside the page will result in a 404 error. For example, the following CURL request will result in a page not found error, irrespective of html5mode:
$ curl http://foo.bar/phones
although the following will return the root/home page:
$ curl http://foo.bar/#/phones
The reason for this is that anything after the hashtag is stripped off before the request arrives at the server. So a request for http://foo.bar/#/portfolio
arrives at the server as a request for http://foo.bar
. The server will respond with a 200 OK response (presumably) for http://foo.bar
and the agent/client will process the rest.
So in cases that you want to share a url with others, you have no option but to include the hashtag.
My solution is create .htaccess and use #Sorian code.. without .htaccess I failed to remove #
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ./index.html [L]