5

I'm trying to remove index.html of the url using html5Mode(true) in angularjs, this is the code:

angular.module('myApp', [
  'ngRoute',
  'myApp.filters',
  'myApp.services',
  'myApp.directives',
  'myApp.controllers'
]).
config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
  $locationProvider.html5Mode(true);
  $routeProvider.when('/home', {templateUrl: 'views/home.html'});
  $routeProvider.when('/about', {templateUrl: 'views/about.html'});
  $routeProvider.otherwise({redirectTo: '/'});
}]);

If I dont write $locationProvider.html5Mode(true); the url shows:

localhost:(port)/MyApplication/index.html

If I write $locationProvider.html5Mode(true); the url shows:

localhost:(port)

MyApplication is removed of the url. And I want the url shows:

localhost:(port)/MyApplication/

What I'm doing wrong? What's the problem?

UPDATE:

How should show my <a> tags? Right now I have:

<a href="#/about">About</>

When I click on the link, the url shows:

localhost:(port)/MyApplication/index.html#/about

I'm lost with this.

Thanks in advance!

2
  • please provide the code where you define the routes. (where you call $routeProvider.when()) Commented Oct 27, 2013 at 12:05
  • @bekite I've updated the post, sorry for the delay. Greetings. Commented Oct 30, 2013 at 8:21

1 Answer 1

13

If your application runs under /MyApplication/ try to set the base path in your index.html and switch on html5Mode:

<html>
  <head>
    <base href="/MyApplication/index.html" />
    ...

See Using $location.

Sign up to request clarification or add additional context in comments.

4 Comments

i was expecting all hrefs like 'dir/file.jpg' to be translated to '/MyApplication/index.html/dir/file.jpg' but to my surprise the browser is doing '/MyApplication/dir/file.jpg'
@Reto Aebersold And when you refresh web page ? problem still here. Using this mode requires URL rewriting on server side, basically you have to rewrite all your links to entry point of your application
@biology.info yes, that's what you have to do in a SPA

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.