diff --git a/src/ng/location.js b/src/ng/location.js
index 1d99e9b2ea21..57fb91178090 100644
--- a/src/ng/location.js
+++ b/src/ng/location.js
@@ -668,6 +668,13 @@ function $LocationProvider(){
if (href.indexOf('://') < 0) { // Ignore absolute URLs
var prefix = '#' + hashPrefix;
if (href[0] == '/') {
+ // Account for base href already present in appBase
+ if (baseHref && href.indexOf(baseHref) === 0) {
+ href = href.substr(baseHref.length);
+ if (!href || href[0] != '/') {
+ href = '/' + href;
+ }
+ }
// absolute path - replace old path
absHref = appBase + prefix + href;
} else if (href[0] == '#') {
diff --git a/test/ng/locationSpec.js b/test/ng/locationSpec.js
index 7e842e16a749..863e7e37a6b8 100644
--- a/test/ng/locationSpec.js
+++ b/test/ng/locationSpec.js
@@ -1330,6 +1330,37 @@ describe('$location', function() {
}).not.toThrow();
});
});
+
+
+ it('should transform the url correctly when a base path is present and html5mode is enabled but not supported', function() {
+ var serverUrl, base;
+ module(function() {
+ return function($browser) {
+ serverUrl = 'http://server';
+ base = '/foo/bar'
+ $browser.url(serverUrl + base);
+ $browser.$$baseHref = base;
+ };
+ });
+ inject(initService(true, '', false), function($rootScope, $compile, $browser, $rootElement, $document, $location) {
+ // we need to do this otherwise we can't simulate events
+ $document.find('body').append($rootElement);
+
+ $rootElement.append('v1v2v3');
+ var av1 = $rootElement.find('a').eq(0);
+ var av2 = $rootElement.find('a').eq(1);
+ var av3 = $rootElement.find('a').eq(2);
+
+ browserTrigger(av1, 'click');
+ expect($browser.url()).toEqual(serverUrl + base + '#/view1');
+
+ browserTrigger(av2, 'click');
+ expect($browser.url()).toEqual(serverUrl + base + '#/baz/view2');
+
+ browserTrigger(av3, 'click');
+ expect($browser.url()).toEqual(serverUrl + base + '#/view3');
+ });
+ });
});