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…


articles
	index.php
	article.php

Here are my two mod_rewrite statements related to my issue…


#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]


#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


	// ******************************
	// 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

DD,

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

Regards,

DK