I like to use Python's SimpleHTTPServer for local development of all kinds of web applications which require loading resources via Ajax calls etc.
When I use query strings in my URLs, the server always redirects to the same URL with a slash appended.
For example /folder/?id=1
redirects to /folder/?id=1/
using a HTTP 301 response.
I simply start the server using python -m SimpleHTTPServer
.
Any idea how I could get rid of the redirecting behaviour? This is Python 2.7.2.
Okay. With the help of Morten I've come up with this, which seems to be all I need: Simply ignoring the query strings if they are there and serving the static files.
The right way to do this, to ensure that the query parameters remain as they should, is to make sure you do a request to the filename directly instead of letting
SimpleHTTPServer
redirect to yourindex.html
For example
http://localhost:8000/?param1=1
does a redirect (301) and changes the url tohttp://localhost:8000/?param=1/
which messes with the query parameter.However
http://localhost:8000/index.html?param1=1
(making the index file explicit) loads correctly.So just not letting
SimpleHTTPServer
do a url redirection solves the problem.I'm not sure how the redirect is generated... I've tried implementing a very basic SimpleHTTPServer, and I don't get any redirects when using query string params.
Just do something like
self.path.split("/")
and process the path before handling the request? This code does what you want I think:Try it out:
HTTP GET /folder/?id=500x
->"FOLDER ID: 500x"
EDIT:
Okay so if you haven't used the SimpleHTTPServer-stuff before, you basically implement the base request handler, implement do_GET(), do_PUT(), do_POST() etc.
What I usually do then is parse the request string (using re), pattern match and see if I can find a request-handler and if not, handle the request as a request for static content if possible.
You say you want to serve static content if at all possible, then you should flip this pattern matching around, and FIRST see if the request matches the file-store and if not, then match against handlers :)