Adding Sub-Section to URL and mod_rewrite

In the past, I was working with URL’s with a “Section” and an “Article Slug” like this…

http://local.debbie/finance/articles/sequester-could-lead-to-more-start-ups

And I had this “permissive” mod_rewrite… (Thanks to the fine people here at SitePoint!!) :tup:


#RewriteRule ((.+)/)?articles/(.+)$ articles/article.php?section=$2&slug=$3 [L]

Stylistically, I like this type of mod_rewrite, because it allows most cases to fall through to my PHP script where I log and display a very specific Error Message (versus a generic 404 error).

Okay, so on to my current problem…

So now I am adding the concept of a “Sub-Section” to my website and thus URL.

New URL’s will look like this…

http://local.debbie/finance/economy/articles/sequester-could-lead-to-more-start-ups

I’m not sure how to modify the mod_rewrite above to give me a similar effect like I have now…

Honestly, I’m not sure what I’m looking for, but I guess I want it so that if either the “Section” or the “Sub-Section” is missing that instead of getting a “Page Not Found”, my mod_rewrite passes the “NULLs” and lets my PHP script decide what to do…

Make sense? :-/

Here is as far as I can get things…


RewriteRule ((.+)/)?((.+)/)articles/(.+)$ articles/article.php?one=$1&two=$2&three=$3&four=$4&five=$5 [L]

Here are the scenarios I get…

Scenario #1:

http://local.debbie/finance/economy/articles/sequester-could-lead-to-more-start-ups


one = finance/
two = finance
three = economy/
four = economy
five = sequester-could-lead-to-more-start-ups

This seems okay.

Scenario #2:

http://local.debbie//economy/articles/sequester-could-lead-to-more-start-ups


one =
two =
three = economy/
four = economy
five = sequester-could-lead-to-more-start-ups

This seems okay.

Scenario #3:

http://local.debbie/finance//articles/sequester-could-lead-to-more-start-ups


one =
two =
three = finance/
four = finance
five = sequester-could-lead-to-more-start-ups

I don’t like this “shifting” of variables

I would have expected…


one = finance/
two = finance
three =
four =
five = sequester-could-lead-to-more-start-ups

Scenario #4:

http://local.debbie///articles/sequester-could-lead-to-more-start-ups


Page Not Found

Not sure I like this either.

I would have expected…


one =
two =
three =
four =
five = sequester-could-lead-to-more-start-ups

With my current code, if either the “Section” or “Article Slug” is missing, things fall through to my PHP code so I can display/log a customized Error Messages.

So, with this new scenario, I would like it so that if either the “Section” or “Sub-Section” or “Article Slug” are missing, then control falls through to my PHP script so I can display/log customized Error Messages. (This should be doable in my mind?!)

Any ideas how to get this behavior?? :-/

Sincerely,

Debbie

Apache seems to collapse the empty path segments before it starts applying the rewrite rules. So GET //economy/articles/sequester-could-lead-to-more-start-ups translates to /filesystem/path/to/DocumentRoot/economy/articles/sequester-could-lead-to-more-start-ups. I suppose you’ll have to use a rewrite condition to capture values from the REQUEST_URI.

EDIT: Frankly, I think the result of these scenarios is acceptable enough to at least launch your site. Then you can get started on refactoring and rewriting so that everything works smoother and easier.

That may be the case, but you were able to help me figure out what I currently have, Jeff, so here is hoping that the solution I was hoping for isn’t very far off.

I would like to think my desire isn’t too big of a deal. I just like to break things down in my code, so when there are issues, I can isolate the problem versus having “catch-all” error handling like most people do.

Also, if I can make my mod_rewrite behave and give me values for the $section, $subsection, and $articleslug, if makes my PHP coding much easier.

I spent quite a bit of time playing around with late last night, but I “throw up the white flag” on this one, because I am a novice with Apache and mod_rewrites at best, and I just don’t have the knowledge to solve this one on my own.

Any help from the peanut gallery would be greatly appreciated!! :wink:

Thanks,

Debbie

Jeff, would it make things easier if I dropped the “articles” from the URL?

Something like this…

http://local.debbie/finance/economy/sequester-could-lead-to-more-start-ups

Then it would just be 3 variables back-to-back like this…

http://local.debbie/{Section}/{SubSection}/{ArticleSlug}

Just another idea… :-/

Sincerely,

Debbie

The problem with scenarios 2, 3 and 4 is that Apache collapses the empty path segments, so it will take some extra trickery to detect them.

Then why did your code below not have the same issue?


RewriteRule ((.+)/)?articles/(.+)$ articles/article.php?section=$2&slug=$3 [L]

If I go from this current URL…

http://local.debbie/finance/articles/sequester-could-lead-to-more-start-ups

…to this one…

http://local.debbie//articles/sequester-could-lead-to-more-start-ups

…it gets passed to my PHP code when I can display a nice custom message.

I feel like I am so close on the new code, yet so far away?!

Debbie