Live data from Hacker News

URL Rewriting for Beginners

addedbytes.com

11–16 of 16 posts

Re: URL Rewriting for Beginners

#11
post #5

Bah, mod_rewrite is for sissies! Parse the url in your handler code! SetHandler mod_python PythonHandler DoStuff Any request starting with /dostuff gets passed to DoStuff handler and you can parse the url pieces in the handler itself: from mod_python import apache def handler(req): req.content_type = 'text/plain' print >> req, 'uri = %s' % req.uri print >> req, 'filename = %s' % req.filename print >> req, 'path_info…

or like this:

    require 'rubygems'
    require 'sinatra'

    get '/:name' do
       "Your name is #{param[:name]}"
    end

Re: URL Rewriting for Beginners

#12
post #6
post #4

It needs to be pointed out that this kind of trick is essentially a workaround for the awful URLs caused by the "the URL is the path of the handler" scheme that Apache and PHP like to encourage. It's almost always a better idea to do this another way. Note that the distinction between the "handler" part of the URL, the remaining "path-like" part and the query string is already exposed in the CGI variables (SCRIPT_NAM…

Agreed. Indeed, most of the rewrites I've seen look like this: RewriteRule .* /index.php

Are you implying that the front-controller pattern is bad?

Re: URL Rewriting for Beginners

#14
An article that tells me how to use Apache is definitely worth 60+ upvotes! Honestly, I can appreciate the effort that went into this tutorial, but I would expect that a "hacker" could get this information just from reading a README or an example htaccess file.

Re: URL Rewriting for Beginners

#15
post #6

Earlier quoted context omitted.

Agreed. Indeed, most of the rewrites I've seen look like this: RewriteRule .* /index.php

Are you implying that the front-controller pattern is bad?

No, but mod_rewrite is not the only way of implementing it. In the mentioned case, index.php would implement a front-controller. mod_rewrite is black magic, and will fail without suggesting why, just throwing a 500 Internal Server Error. Debugging a front controller in PHP (or whatever) it much easier.

Re: URL Rewriting for Beginners

#16
I am very new to URL rewriting myself, so I have a pretty dumb question.

How does mod rewrite affect GET operations in PHP? If you rewrite the URL: http://www.example.com?id=1 to http://www.example.com/1/

And your code uses GET to grab the id from the URL. Does this still work? Or is the mod rewrite processed afterwards?

Post reply on HN