SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Create a Catch-All PHP File - How?

  1. #1
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Create a Catch-All PHP File - How?

    Is is possible to create a PHP file in my site's root folder that catches all requests to URLs that don't match other files or subfolders?

    I want to use this technique to implement MVC in my project. Instead of specifying which module and method to call in the query string, I want request to be sent to my catch-all file.

    Example: If someone requested "http://www.exampledom/foo/bar" it would be handled by my PHP catch-all script that would split out the "foo" and "bar" and, from my code conventions and design, know what to do to respond.

    Thanks for the help,
    Stephen Martindale

  2. #2
    SitePoint Addict The Mog's Avatar
    Join Date
    Dec 2002
    Location
    Manchester UK
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not 100% sure but i think its something to do with a .htaccess file and mod rewrite


    Do a google on this and you should find the answer

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    New Castle, PA
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can break the address up into an array using
    Code:
    //GET FOLDER PATH NAME
    $path = explode('/',$_SERVER['REQUEST_URI']);
    depending on how many sub-folders you have, you can adjust accordingly

    for example, if you ran a print_r($path) on the above, and you put the above in a script called

    http://www.example.com/foo/bar/path.php

    you would get

    Array([0]=>[1]=>foo[2]=>bar[3]=>path.php)

  4. #4
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks jreider, you solved the second part of my problem.

    Which PHP file would I put this line into?

  5. #5
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the tip, Mog. I Googled for ".htaccess file and mod rewrite" and found exactly what I need. Basically, it tells me that I can enable URL rewriting AFTER I have finished coding without it, without having to change my working design.

    This is not a problem until then, then. Thanks

  6. #6
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the mod_rewrite.....
    Code:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond $1 !-d
    RewriteRule ^(.*)$ /index.php?path=$1 [QSA,L]
    That will redirect all requests not on the filesystem to index.php with the GET variable path containing the rest of the url.

  7. #7
    SitePoint Zealot basbd's Avatar
    Join Date
    Oct 2005
    Location
    Oregon, USA
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could also simply set your catch all script as your error document:

    Code:
    ErrorDocument 404 catch-all.php
    and then use jreider advice for gathering the requested url

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
  •