AngularJS as user-accessible URL routing?

Basic routing method as shown below,

angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives', 'myApp.controllers']).
 config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
  $routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: 'MyCtrl1'});
  $routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: 'MyCtrl2'});
  $routeProvider.otherwise({redirectTo: '/view1'});
  $locationProvider.html5Mode(true);
}]);

      

I want the user to be able to access view1 by typing url ": // host / view1" in the browser. but it doesn't work.

Using $ routeProvider and $ locationProvider if user clicks "href = / view1" this link then page routed host / view1.

But if the user accesses the url ": // host / view1" directly, it emits a 404 error. In order for the user to access / view 1 directly from the url, I need to remove the html5Mode, then the user can access: // host / # / view1 "

but how can I remove the #! character? not only html5mode only, user can access the page directly by typing this url.

0


source to share


2 answers


Thank. Yoshi

I create .htaccess file in DocumentRoot folder and add below

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]

      

to the .htaccess file.



works as i expected!

ps.

RewriteCond     %{REQUEST_FILENAME} !-f
RewriteCond     %{REQUEST_FILENAME} !-d

      

above two lines means that the file with the specified name does not exist in the browser or the directory does not exist in the browser, and then processes the rewrite rule below

+2


source


For Java application deployed to Tomcat, we will need to rewrite the url using Tuckey as



<urlrewrite default-match-type="wildcard">
<rule>
        <from>/home</from>
        <to>/index.html</to>
    </rule>
</urlrewrite>

      

0


source







All Articles