0

When working with Angular and its routes, if you reload the page in, let's say, localhost:9000/products, the response will be a 404.

I am using Python server created using python -m SimpleHTTPServer port no. How to solve this problem since .htaccess file is not working in this?

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

.htaccess files are for apache http server not the python server, the .htaccess is for setting up redirects that the apache server will observe but if you use nginx or in this case python simple http server you'd have to use redirects specific to that particular http server this may help:

https://gist.github.com/chrisbolin/2e90bc492270802d00a6

copied here not written by myself apparently also from SO

''' Taken from: http://stackoverflow.com/users/1074592/fakerainbrigand http://stackoverflow.com/questions/15401815/python-simplehttpserver '''

import SimpleHTTPServer, SocketServer import urlparse, os

PORT = 3000 INDEXFILE = 'index.html'

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:
        # send index.html, but don't redirect
        self.send_response(200)
        self.send_header('Content-Type', 'text/html')  
        self.end_headers()
        with open(INDEXFILE, 'r') as fin:
          self.copyfile(fin, self.wfile)

Handler = MyHandler

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

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

Also personally I use apache locally and just have browsersync proxy to the apache server and that handles the redirect if a file isn't found, from there the angular page takes over and routing kicks in to restore the view or go to a page not found view.

shaunhusain
  • 19,630
  • 4
  • 38
  • 51