How to remove index.html from url on website based on angularjs

前端 未结 4 2179
醉梦人生
醉梦人生 2021-02-19 07:21

I didn\'t find a way to remove index.html from the url, because like this looks really ugly.

mydomain.com/index.html#/myview1 
mydomain.com/index.html#/myview2
<         


        
相关标签:
4条回答
  • 2021-02-19 07:37

    Use

    <rewrite>
        <rules>
            <rule name="RewriteRules" 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="/index.html" />
          </rule>
      </rules>
    </rewrite>
    

    And on Index page

    <base href="/" /> 
    

    And on your config use

    $locationProvider.html5Mode(true);
    

    It should work

    0 讨论(0)
  • 2021-02-19 07:44

    Maybe this approach could help:

    Add $locationProvider.hashPrefix();, which removes index.html in your URL, in your app.js config. Don't forget to add the new dependency. After that your app.js could look similar to this:

     angular.module('myApp', [
      'ngRoute'
    ]).
    config(['$locationProvider', '$routeProvider', function($locationProvider, $routeProvider) {
      $locationProvider.hashPrefix(); // Removes index.html in URL
    
      $routeProvider.otherwise({redirectTo: '/someroute'});
    }]);
    

    Note that this does not remove the hashtag. You can find your new route at: .../app/#/someroute

    0 讨论(0)
  • 2021-02-19 07:50

    activate the html5mode for $location

    0 讨论(0)
  • 2021-02-19 07:51

    You'll have to do some work on the server side to set this up. html5mode gets you part of the way, but as soon as the user refreshes, the reference to the file will be wrong. You want links that are persistent and can be shared, presumably. Basically make sure any request to url.com/myapp/{etc} (or whatever) gets redirected to index.html and let your angular app sort out the routing.

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