Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1

    htaccess, to catch all possible 404 requests, without ErrorDocument directive.

    Hi friends,

    I've been trying to make a model/view structure for a website (or the aedrin's method ), so my root directory structure is
    Code:
     
     model/
     view/
     common/
     classes/
     css/
     downloads/
     images/
     javascript/
     
     index.php
     .htaccess
    I've
    Code:
    RewriteRule ^([^/\.]+)(/*)([^/\.]*)$ index.php?page=$1&rest=$3 [L]
    in my htaccess, which will convert all links like /about, /contact to ?page=about, ?page=contact and so on.

    And my index.php contains
    PHP Code:
    $page=trim($_GET['page']);
    if(
    $page=="" || $page=="index.php")
        
    $page='home';
    $general_pages=array("home","about","contact","contact_webmaster");

    if(!
    in_array($page,$general_pages))
        
    $page="404";


    include(
    "model/{$page}.php");
    include(
    "common/header.php");
    include(
    "common/nav.php");
    @include(
    "view/{$page}.php");
    include(
    "common/footer.php"); 
    The above code also works for /download/file1 to download a file named file1 (I've the required code in /model/download.php, $file=$_GET['rest']; .........)

    Now, this setup works fine for all /blah-blah type links, which will show a text in the /view/404.php
    Code:
    <p> Sorry, the requested page is not found in site. In case, if you have followed a valid link,
    please <a href="/contact_webmaster/page=blah-blah&rest=">let know the Majlis webmaster</a>.
    </p>
    and thus I can catch the requested url in my contact_webmaster form.

    Now my question are
    1. Can I catch requests (by modifying htaccess) like /about.php, /blah.blah etc.(currently showing an apache 404 error) in my index.php and pass through the rest of codes?
    2. How can I restrict request like /model/about.php, /view/about.php etc. which is currently displaying a php error or some html with no styles


    Hope my questions are clear.
    After all sorry for this huge post and thank you for reading the same (I've no other way to explain)

    regards,
    raoof.
    Last edited by abduraooft; 05-01-2008 at 08:48 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Can I catch everything after my domain name to a variable?
    Something like
    Code:
    RewriteRule ^(.*)$ index.php?page=$1 [L]
    ?

    Unfortunately the above one is not working :-(
    Please correct me if I'm wrong.

    thanks.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    120 views... no reply :-(
    I must be doing very stupid . Hope my questions were clear.

    For the time being when I added
    Code:
    ErrorDocument 404 /view/404.php
    I could manage to show a page with no style and a link to the contact_webmaster page for requests like /blah.blah

    And when I added
    Code:
    RewriteRule ^(model|view|classes)/(.*)$ index.php?page=$2 [L]
    to the above, I think, my problem is solved for about 90% (rest 10 for unidentified problems and for the inability to track the requested page in some cases)
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Hmm.. this thread is going to be an addition to my other thread (101 ways to post....)

    OK, now a different question..
    Can I simplify
    Code:
    RewriteRule ^tax_exemption$ /view/tax_exemption.php  
    RewriteRule ^staff_details$ /view/staff_details.php 
    RewriteRule ^affiliation_details$ /view/affiliation_details.php 
    RewriteRule ^map_location$ /view/map_location.php
    
    RewriteCond %{REQUEST_FILENAME} !tax_exemption
    RewriteCond %{REQUEST_FILENAME} !staff_details
    RewriteCond %{REQUEST_FILENAME} !affiliation_details
    RewriteCond %{REQUEST_FILENAME} !map_location
    RewriteRule ^(model|view|classes)/(.*)$ index.php?page=$1&rest=$2
    The idea is I've some pages which are decided to open as popups. So they don't have no common header/footer/nav blocks like other pages have. So I just placed them inside the view/ folder with full document structures.

    I'd already restricted access to all files inside this directory via direct urls, but I need to allow this specific cases.

    The above rules work as desired, but can I simplify this?
    Any help would be much appreciated.

    thanks
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

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