mod_rewrite the drupal way?

Discussion in 'Apache' started by carolina-advertising, Nov 7, 2007.

  1. #1
    Hey guys,
    I was trying to figure out how drupal lets you define a url alias and it is stored in MySQL. Then the alias links are printed to browser but at click it is grabbed from mysql and then re-written with mod_rewrite...

    And here is all that is in htaccess

    # Rewrite current-style URLs of the form 'index.php?q=x'.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

    I cannot seem to begin how to figure this out :(
    ~R
     
    carolina-advertising, Nov 7, 2007 IP
  2. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    The rules you posted above basically says, if there is a request for a file that doesn't exist, rewrite the url to index.php?q=$1, where $1 is the non-existent file name.

    So if you are requesting for http://www.domain.com/abc and file 'abc' doesn't exist, it would be rewritten so that it's as if you're requesting http://www.domain.com/index.php?q=abc.

    You'll have to have a look at the index.php file to see how it maps the request to a row in the MySQL database.
     
    phper, Nov 7, 2007 IP
  3. carolina-advertising

    carolina-advertising Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well,
    There is only a few lines in index. The modules are GaLore!
    I've tried tracking down to request and no go so far.
    But thanks,
    ~RD
     
    carolina-advertising, Nov 7, 2007 IP
  4. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    The index.php could just be the 'controller' file that simply forwards the request to another script.

    In terms of the URL rewriting, it simply rewrites any file that is non-existent to index.php?q=theNameOfThatNonExistentFile. From then on it's all PHP. If you need help understanding what index.php does you can always ask in the PHP forum.
     
    phper, Nov 8, 2007 IP
  5. beejaysea

    beejaysea Peon

    Messages:
    141
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Are you trying to figure out how to do this in general, or how Drupal does it specifically?

    Drupal's index.php loads the boostrap process. The bootstrap process initializes all the various modules that are enabled.

    In general, Drupal writes urls such as ?q=node/2, meaning module 'node', parameter '2', which then hits up the node module's hooks to do 'something' with '2'. Default being 'view'. ?q=node/2/edit would tell the node module to go through the process of attempting to edit a node, if the user has permissions, they would be allowed to do so.

    Let's assume that you have clean URLs enabled, these would be /node/2, and /node/2/edit respectively.

    Assuming your original question, how does it rewrite URLs, the 'path' module must be enabled, and there must be an entry for that specific URL in the database. There's a 'src' and a 'dst' field in the url alias table that maps these. (source and destination).

    If, for instance, you are mapping /great-url-here to /node/2, at some point, the path module will be invoked, given that there is no module named 'great-url-here', and the path module will then ask the database, "are there any mappings for great-url-here?", if so, it presents (internally) the content on /node/2.

    If you are interested in the guts of Drupal, or just want to understand how things work with it, PRO Drupal Development is THE book to get. It goes over the entire bootstrap process in detail, as well as a boatload of other great things.

    Hopefully that was sensible enough to follow.

    If you are just trying to write this on your own, as phper says, just grab the query string somehow, and map your query to your content.
     
    beejaysea, Nov 9, 2007 IP
  6. carolina-advertising

    carolina-advertising Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Well,
    You said that in a way that I understood!
    I have tried to track it. I also figured out the SRC and DST in htaccess but I still can't seem to see how it is passed to htaccess with this in the database. unless the code in htaccess is sending the request in a U turn back to drupal to write it internally. Did I say that right? - lol

    thanks,
    RD
     
    carolina-advertising, Nov 15, 2007 IP
  7. beejaysea

    beejaysea Peon

    Messages:
    141
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yes, effectively it's being re-written internally. All the magic is in path.module.

    What is it you're trying to accomplish anyhow? :)
     
    beejaysea, Nov 15, 2007 IP
  8. carolina-advertising

    carolina-advertising Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I've got some MFA sites to do for someone that is template driven and I was doing the rewrite by hand in htaccess. Later on I had to do an install for a client with drupal. I noticed how the path was working from mysql entries but I just couldn't grasp how the request is sent etc for the rewrite.

    Did I get that last post right?

    The path module is called from htaccess when no match for a static file is found with the SRC DST and then htaccess sends request back to the code where path module then looks in mysql and sends the alias to browser?

    EDIT, I mean after the U turn in htaccess, path grabs SRC and leaves it in address bar and send DST to browser! :) right?


    Bleh... Am on to it?


    Thanks,
    RD
    EDIT, I was going to make a mysql driven cms just for MFA sites so I wanted to learn how to rewrite them like drupal from mysql into .htm links :)

    Also, I think CMS's are all over coded and full of 10000's of lines not needed. I like writing php custom backends that is simple and blistering fast.
    So thats is why I try to always learn the nitty gritty :) to cut out all the extra not needed code etc.
     
    carolina-advertising, Nov 15, 2007 IP