0

I have an angularjs app that uses Angular UI Router and the URL that are created have a # in them.. Eg. http://localhost:8081/#/login. I am using Python Simple HTTP server to run the app while developing. I need to remove the # from the URL. I know how to remove it by enabling HTML5 mode in angular. But that method has its problems and i want to remove the # from the server side.

How can i do this using Python Simple HTTP Server?

Kasun Kodagoda
  • 3,956
  • 5
  • 31
  • 54
  • 2
    possible duplicate of http://stackoverflow.com/questions/16677528/location-switching-between-html5-and-hashbang-mode-link-rewriting see also https://scotch.io/quick-tips/pretty-urls-in-angularjs-removing-the-hashtag –  Apr 29 '15 at 09:11
  • No its not. I know how to remove it from angularJS. but as i said, It creates problems. I have faced it. Take a look at [this](http://stackoverflow.com/questions/29936224/page-reload-fails-when-using-angular-ui-router-with-html5-mode-enabled) – Kasun Kodagoda Apr 29 '15 at 09:20
  • Ah sorry, I didn't read properly. I have been struggling with this issue with Python Simple HTTP Server myself, I did not find a solution. I did however manage to do it with NodeJS http-server. –  Apr 29 '15 at 09:29
  • Question is not clear. Url string is client side property and all that server can do - change the logic how to parse provided route, but not change url on the client side. Here is only one visibly for me way: generate 301 HTTP response of the server with fixed url, but there no guarantee that angularjs will not append # again. – Reishin Apr 29 '15 at 11:57

3 Answers3

1

You can derive from BaseHTTPRequestHandler and override the do_GET method.

class MyHTTPHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        # do your own processing further, if you want to pass it back to
        # default handler with the new path, just do the following
        self.path = self.path.strip('/#')
        BaseHTTPRequestHandler.do_GET(self)
nighthawk
  • 11
  • 1
0

Well i had a similar problem but the difference is that i had Spring on the Server Side.

You can capture page not found exception at your server side implementation, and redirect to the default page [route] in your app. In Spring, we do have handlers for page not found exceptions, i guess they are available in python too.

Manish Kr. Shukla
  • 4,447
  • 1
  • 20
  • 35
-3

You could catch the string that represends the webadres adres, and if there is a # in replace it with a empty character.

stringA = 'http://localhost:8081/#/login'
stringB = stringA.replace("#", "")
print(stringB)
Tenzin
  • 2,415
  • 2
  • 23
  • 36
  • Yes. If you don't like my answer, well sorry. But I am just trying to help. And there are more roads to Rome. :-) – Tenzin Apr 29 '15 at 09:26
  • I know you are trying to help. Your answer doesn't make sense that's why i asked. I wanted to remove the # from the URL when it runs on the SimpleHTTPServer just like it can be done in IIS and Apache. I have no knowledge in python so if this is a part of a larger answer add it. – Kasun Kodagoda Apr 29 '15 at 09:32
  • Then why did you add a python tag to it? – Tenzin Apr 29 '15 at 09:34
  • You should add an explanation to your answer. – deW1 Apr 29 '15 at 10:14