News for hackers. Stuff that matters.
Re:URL Rewriting for Beginners by snorkel on Monday August 04, 2008 @12:11PM (#266399)
Bah, mod_rewrite is for sissies! Parse the url in your handler code!

   <Location /dostuff>
     SetHandler mod_python
     PythonHandler DoStuff
   </Location>
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 = %s' % req.path_info
     return apache.OK
Re:URL Rewriting for Beginners by sant0sk1 on Monday August 04, 2008 @09:43PM (#267033)
or like this:

    require 'rubygems'
    require 'sinatra'

    get '/:name' do
       "Your name is #{param[:name]}"
    end
Re:URL Rewriting for Beginners by ajross on Monday August 04, 2008 @11:44AM (#266370)
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_NAME, PATH_INFO, and QUERY_STRING). The handlers can do this magic on their own. Doing it by placing important application code into the apache configuration for the site strikes me as very confusing.

Re:URL Rewriting for Beginners by mseebach on Monday August 04, 2008 @12:57PM (#266449)
Agreed. Indeed, most of the rewrites I've seen look like this: RewriteRule .* /index.php
Re:URL Rewriting for Beginners by samataro on Tuesday August 05, 2008 @01:13AM (#267185)
Are you implying that the front-controller pattern is bad?
Re:URL Rewriting for Beginners by mseebach on Tuesday August 05, 2008 @05:09AM (#267328)
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 by iamelgringo on Monday August 04, 2008 @01:41PM (#266503)
Wow, I suppose that this is a big issue for people, isn't it. I'd forgotted since I'm using Django. Django makes you craft your URLs from the start. Granted, you can still create crap URLs if you really want to, but Django makes it harder to do.
Re:URL Rewriting for Beginners by markbao on Monday August 04, 2008 @07:18PM (#266904)
I used to do this with my flat file PHP files, but then I started using a framework. I suggest you do too.

Kohana (PHP Framework) has pretty good URL rewriting resembling RoR's. /controller/method/variable. (/user/edit/1005 would reference application/controllers/user.php, class User_Controller, function user(1005).) POST/GET/PUT/etc are retained.

Re:URL Rewriting for Beginners by gm on Monday August 04, 2008 @10:20AM (#266274)
I'm part of the "I don't do this myself, but I manage people who do" crowd. It's always good to see these types of articles on here. They keep us from being totally ignorant about the mechanics of our own projects. Thanks!
Re:URL Rewriting for Beginners by biohacker42 on Monday August 04, 2008 @10:25AM (#266279)
Doing that with web.py is easy and fun, takes at most half an hour, worth playing with at least once.
Re:URL Rewriting for Beginners by Frocer on Tuesday August 05, 2008 @05:15PM (#267896)
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?

Re:URL Rewriting for Beginners by BrandonM on Tuesday August 05, 2008 @03:54AM (#267279)
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 by seekely on Tuesday August 05, 2008 @02:37AM (#267229)
http://forum.modrewrite.com/ has really helped me a couple of times. Just about any reasonable rewrite scenario you can think of has been asked and answered somewhere in the archive.