AngularJS: How to remove #!/ (bang prefix) from URL?

后端 未结 6 1350
不思量自难忘°
不思量自难忘° 2020-12-03 15:29

I already have done $locationProvider.html5Mode(true); but it is not working. Whenever I access http://example.com it goes to http://example.

相关标签:
6条回答
  • 2020-12-03 16:05

    If you want to remove this prefix, add this code to your config:

    appModule.config(['$locationProvider', function($locationProvider) {
      $locationProvider.hashPrefix('');
    }]);
    

    Source here for more information.


    Update

    If you want to remove the whole prefix (# and not only !), you may try this solution:

    1) Activate the HTML5 mode and remove the prefix ! in your module config

    $locationProvider.html5Mode(true);
    $locationProvider.hashPrefix('');
    

    2) And then set base to / in the <head> on your index.html

    <head>
        ...
        <base href="/">
    </head>
    
    0 讨论(0)
  • 2020-12-03 16:07

    Add to your html file <head> <base href="/foldername/"> </head>

    and this one is for your app.js $locationProvider.html5Mode(true);

    this is for .htaccess create file if you don't have.

    RewriteEngine On 
    # If an existing asset or directory is requested go to it as it is
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d  
    RewriteRule ^ - [L]
    RewriteRule ^ /foldername/
    
    0 讨论(0)
  • Below code will remove exclamatory (!) mark in URL

    $locationProvider.hashPrefix('');
    

    or else go to Routing issue with AngularJS project using yeoman setup

    it worked for me

    0 讨论(0)
  • 2020-12-03 16:12

    Add the following to app.js

    app.config(['$locationProvider', function($locationProvider) {
    
      $locationProvider.hashPrefix('');
    
    }]);
    
    0 讨论(0)
  • 2020-12-03 16:12

    If you are in .NET stack with MVC with AngularJS, this is what you have to do to remove the '#' from url:

    1.Set up your base href in your _Layout page:

     <head> <base href="/"> </head>
    

    2.Then, add following in your angular app config :

    $locationProvider.html5Mode(true)
    $locationProvider.hashPrefix("!");
    
    0 讨论(0)
  • 2020-12-03 16:12
    $locationProvider.hashPrefix('');
    

    Please check this question: AngularJS ui-router application has /#!/ in the URL

    0 讨论(0)
提交回复
热议问题