SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast konky2000's Avatar
    Join Date
    Mar 2003
    Location
    Oakland
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mod_rewrite or PHP?

    Does it make sense to use PHP for the parsing of URLs in place of using a bunch of mod_rewrite rules?

    For a few years now I have been managing my URLs using mod_rewrite. I will generally have about 10-12 rules to take care of various combinations of possible GET variables.

    Code:
    RewriteRule ^$ page.php?sub=Home&view=Default&url=%{REQUEST_URI} [L]
       RewriteRule ^([A-Z][A-Za-z]+)/$ page.php?sub=$1&view=Home&url=%{REQUEST_URI} [L]
       RewriteRule ^([A-Z][A-Za-z]+)/([A-Z][A-Za-z0-9]+)/$ page.php?sub=$1&view=$2&url=%{REQUEST_URI} [L]
     RewriteRule ^([A-Z][A-Za-z]+)/([A-Z][A-Za-z0-9]+)/([1-9][0-9]{3})/$ page.php?sub=$1&view=$2&year=$3&url=%{REQUEST_URI} [L]
    But now I am wondering if it makes more sense to just have one mod_rewrite rule like the following:

    Code:
    RewriteRule ^$ page.php?url=%{REQUEST_URI} [L]
    And then use PHP code on my page.php file to parse the values within the $_GET['url'] string.

    Any opinions on whether it is better to use mod_rewrite sparingly and do the majority of URL parsing within PHP itself?
    Konky 2000 Collections - Japanese stickers and floaty pens

  2. #2
    Non-Member DaveMichaels's Avatar
    Join Date
    Nov 2004
    Location
    US
    Posts
    535
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't understand why you think you need to pass the URL again. I just use vars like view, id, sort, page, etc.

    I've used both mod_rewrite and PHP for dealing with URLs and prefer mod_rewrite far far far better.

  3. #3
    SitePoint Enthusiast konky2000's Avatar
    Join Date
    Mar 2003
    Location
    Oakland
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the first case I use the URL to autogenerate breadcrumbs. In the second case, I would pass the URL so that my PHP script could use it to get the necessary pages.

    I am leaning more towards sticking with mod_rewrite myself. It certainly does things more efficiently than I can imagine PHP. But I figure there is always going to be an opposing opinion, and I'd like to hear it if it exists...
    Konky 2000 Collections - Japanese stickers and floaty pens

  4. #4
    SitePoint Addict pointbeing's Avatar
    Join Date
    Jun 2004
    Location
    London, UK
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's certainly true that you don't need to tag the original URL back onto the rewritten URL though. If you have a look at your phpinfo() output you can see that, after rewriting, the originally requested URL will still be available in $_SERVER['REQUEST_URI'], or in $_SERVER['REDIRECT_URL'] without the query string.

    My own approach is just to set up one rewrite rule pointing everything to Main.php, then parse the REDIRECT_URL very simply in my Request class.

    example.com/login
    => new LoginDefaultCommand();
    $request->getModule() returns 'login'
    $request->getAction() returns 'default'
    $request->getId() returns NULL

    example.com/property/list
    => new PropertyListCommand();
    $request->getModule() returns 'property'
    $request->getAction() returns 'list'
    $request->getId() returns NULL

    example.com/property/details/30
    new => PropertyDetailsCommand();
    $request->getModule() returns 'property'
    $request->getAction() returns 'details'
    $request->getId() returns 30


    Which is quite nice, because we can then set up auth policies for a whole module, for a given action, or just a specific resource. Also breadcrumbs become a trivial task. Downside is you do need to set up your own 404 handling.

    But I'm rambling here The point is that since the URLs are so simple, there's really no need for any complex ?page=newspage&action=domyaction&foo=bar rewrite rule nonsense

  5. #5
    SitePoint Guru asterix's Avatar
    Join Date
    Jun 2003
    Posts
    847
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My own approach is just to set up one rewrite rule pointing everything to Main.php, then parse the REDIRECT_URL very simply in my Request class.
    OK, but what do your URLs look like?
    page.php?var1=val1&var2=val2&var3=val3 or

    x.com/val1/val2/val3/val4.html

    Do you stick to one method or do you mix and match and then have the request class sort out the spaghetti?

  6. #6
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Moved to a more appropriate forum. See Where should I post my thread? for details

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  7. #7
    SitePoint Addict pointbeing's Avatar
    Join Date
    Jun 2004
    Location
    London, UK
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by asterix
    OK, but what do your URLs look like?
    page.php?var1=val1&var2=val2&var3=val3 or

    x.com/val1/val2/val3/val4.html
    Nothing like that - just like those I posted: /login or /property/details/30 /user/register or whatever. Rarely more than 3 parts, and we never expose the underlying programming language.

    Searches are a bit different - here you'll often want to put in a query string with the search term and the page number and so on. We'll always use GET for that sort of thing so searches can be bookmarked and so forth.

    Quote Originally Posted by asterix
    Do you stick to one method or do you mix and match and then have the request class sort out the spaghetti?
    Always the one method (since I've been dev lead , at least), and there's no spaghetti to sort out, to be honest. Just a simple explode() on the URI, a little like so:

    PHP Code:
    <?php

    class Request {

        
    // ...

        /**
        *  Extracts parameters from page uri
        *
        *  @access protected
        */
        
    protected function _parseUrl()
        {
            
    $rpath explode('/'preg_replace('/(^\/|\/$)/'''$_SERVER['REDIRECT_URL']));

            
    // loop through URL
            
    for ($ctr 0$param array_shift($rpath); $ctr++) {
                
    $this->params[$ctr] = $param;            
            }
        
            
    // which module and action
            
    $this->params['module'] = (isset($this->params[0])) ? $this->_paramFormat($this->params[0]) : self::DEFAULT_MODULE;
            
    $this->params['action'] = (isset($this->params[1])) ? $this->_paramFormat($this->params[1]) : self::DEFAULT_ACTION;
            
    $this->params['id']     = (isset($this->params[2])) ? $this->_paramFormat($this->params[2]) : NULL;
        }
    }

    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •