angularjs pretty url is not working

可紊 提交于 2019-12-31 05:16:08

问题


My problem is url is not working without # . Here is my code :

angular.module('Hiren', ['ngRoute'])
.config(function ($routeProvider, $locationProvider) {
    $routeProvider
        .when('/', {
            templateUrl: 'partials/login.html',
            controller: 'login'
        })
        .when('/redirect', {
            templateUrl: 'partials/postLogin.html',
            controller: 'postLogin'
        });

    $locationProvider.html5Mode(true);
});

When I try to browse the url example.com/redirect , it gives me 404 , but using hash ( example.com/#redirect) its perfectly working .


回答1:


If you're getting a 404 in HTML5 mode you need to configure your server to serve your Angular application (usually index.html or app.html) when a 404 happens on the server. You don't mention what you're using for a server so I can't give specific instructions on how to do that. This is more a server configuration issue than an Angular one.

Edit now that server is known:

import SimpleHTTPServer, SocketServer
import urlparse, os

PORT = 3000

class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
   def do_GET(self):

       # Parse query data to find out what was requested
       parsedParams = urlparse.urlparse(self.path)

       # See if the file requested exists
       if os.access('.' + os.sep + parsedParams.path, os.R_OK):
          # File exists, serve it up
          SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self);
       else:
          # redirect to index.html
          self.send_response(302)
          self.send_header('Content-Type', 'text/html')  
          self.send_header('location', '/index.html')  
          self.end_headers()

Handler = MyHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT
httpd.serve_forever()

Referenced SO answer.



来源:https://stackoverflow.com/questions/25064738/angularjs-pretty-url-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!