SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,528
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Problem with Section/Article in mod_rewrite

    I added the concept of "Sections" to my website, but am trying to make them "virtual" so that I can keep my current Directory/File structure of this...

    Code:
    articles
    	index.php
    	article.php

    Here are my two mod_rewrite statements related to my issue...
    Code:
    #PRETTY:		finance/
    #UGLY:			articles/index.php?section=finance
    
    #Rewrite only if the request is not pointing to a real file (e.g. add_comment.php, index.php).
    RewriteCond %{REQUEST_FILENAME} !-f
    
    #Match any kind of Section.  PHP will decide if it's valid or not.
    RewriteRule (.+)/$ articles/index.php?section=$1 [L]

    Code:
    #PRETTY:		articles/postage-meters-can-save-you-money
    #UGLY:			articles/article.php?slug=postage-meters-can-save-you-money
    
    #Rewrite only if the request is not pointing to a real file (e.g. add_comment.php, index.php).
    RewriteCond %{REQUEST_FILENAME} !-f
    
    #Match any kind of slug.  PHP will decide if it's valid or not.
    RewriteRule articles/(.+)$ articles/article.php?slug=$1 [L]

    And here is a stripped down version of article.php...
    PHP Code:
        // ******************************
        // Attempt to Retrieve Article.    *
        // ******************************
        
    if (isset($_GET['slug']) && $_GET['slug']){
            
    // Slug found in URL.


            // ****************************
            // Check Article-Slug Format.    *
            // ****************************
            
    if (preg_match('~(?x)                            # Comments Mode
                        ^                            # Beginning of String Anchor
                        (?=.{2,100}$)                    # Ensure Length is 2-100 Characters
                        [a-z0-9_-]*                    # Match only certain Characters
                        $                            # End of String Anchor
                        ~i'
    $_GET['slug'])){
                
    // Valid Article-Slug Format.

            
    }else{
                
    // Invalid Article-Slug Format.
                
    $_SESSION['resultsCode'] = 'ARTICLE_INVALID_SLUG_FORMAT_2271';

                
    // Set Error Source.
                
    $_SESSION['errorPage'] = $_SERVER['SCRIPT_NAME'];

                
    // Redirect to Display Outcome.
                
    header("Location: " BASE_URL "/account/results.php");

                
    // End script.
                
    exit();
            }
    //End of CHECK ARTICLE-SLUG FORMAT

        
    }else{
            
    // Slug Not found in URL.
            
    echo "ARTICLE-SLUG NOT FOUND";
            exit();

            
    //THIS CURRENTLY DOESN'T FIRE BECAUSE OF SECTION REWRITE

            // Redirect to Display Outcome.
            
    header("Location: " BASE_URL "/articles/index.php");

            
    // End script.
            
    exit();

        }
    //End of ATTEMPT TO RETRIEVE ARTICLE 

    In my new format, and Article would appear at a URL like this..

    http://local.debbie/finance/articles/postage-meters-can-save-you-money


    The problem I am having, is that my mod_rewrite and the addition of "Sections" seem to be messing up my PHP Error-Handling which was originally written before I introduced the concept of "Sections"...


    For example, if I change the URL to this...

    local.debbie/articles/article.php?slug=

    ...then the following Error-Message is displayed...
    ARTICLE-SLUG NOT FOUND
    This is correct!!


    But if I change the URL to represent what it would look like with a Section...

    local.debbie/finance/articles/article.php?slug=

    ...then the following Error-Message is displayed...
    Article Not Found

    The article you chose cannot be found. (2271)
    This is wrong!!


    In this second scenario, the "slug" is still NULL, so the "ARTICLE-SLUG NOT FOUND" branch should still fire, but adding "finance" to the URL seems to break things.

    (BTW, as a rule of thumb, my mod_rewrites are usually "permissive" so that things pass through to my PHP where it can better handle errors...)

    So what is going on here?!

    Thanks,


    Debbie

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,604
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    DD,

    Both of your mod_rewrite code snippets are in the .htaccess? If so, it's a question of order.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


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
  •