mod_rewrite collisions

I am in mod_rewrite hell right now… :frowning:

My website uses “pretty URL’s” and everything has been working thanks to a lot of help in the past from Jeff Mott. :tup:

Unfortunately, I added a new feature that seems to be conflicting with one of my main mod_rewrites that in retrospect, I don’t think is very well written. (My fault, not Jeff’s!!)

Problem Described:
My website has Articles about small-businesses and every Article has a Section and a Sub-Section.

Because I can’t know in advance what the Sections and Sub-Sections will be, I left my mod_rewrite very generic…


# SHOW ARTICLE
#PRETTY:	finance/tax-season/saves-your-taxes-for-a-cpa
#UGLY:		articles/article.php?section=finance&subsection=tax-season&article=saves-your-taxes-for-a-cpa

#Rewrite only if the request is not pointing to a real file.
RewriteCond %{REQUEST_FILENAME} !-f

#Match any kind of Section, Subsection and Article.  PHP will decide if it's valid or not.
RewriteRule (.+)/(.+)/(.+)$ articles/article.php?section=$1&subsection=$2&article=$3 [L]

Here is where the problem came in tonight…

In my “Private Message” module, I want to allow Users to sort Messages by each column (e.g. Sender, Subject, Date), as well as having the ability to specify Ascending/Descending sorting.

This preliminary mod_rewrite was working…


RewriteRule account/messages/((.+)/)?(.+)?$ account/messages.php?msgview=$2&sortname=$3 [L]

But now that I have added one more parameter, things are conflicting with my very loose Article mod_rewrite above…

I can’t get this one working…


RewriteRule account/messages/((.+)/)?((.+)/)?(.+)?$ account/messages.php?msgview=$2&sortname=$4&sortdir=$5 [L]

To back up a bit, I tried this one…


RewriteRule account/messages/((incoming|sent|trash)/)?((.+)/)?$ account/messages.php?msgview=$2&sortname=$4 [L]

This URL works…


http://local.debbie/account/messages/incoming/by-date/

But this one bleeds over into my Article mod_rewrite…


http://local.debbie/account/messages/incoming/by-date

I am almost wondering if in my Article mod_rewrite, I need to define what does NOT constitute an Article, for example…


http://local.debbie/account/messages/incoming/by-date

Not sure if any of this makes any sense?! :frowning: :frowning: :frowning:

Sincerely,

Debbie

Once upon a time, all your articles used to be under an “articles/” path. There was no doubt about what was or wasn’t an article. But the problem now is how is your code supposed to know whether “account” or “finance” are article sections? If the articles rewrite is going to be a generic catch-all, then you need to make sure that all the other, more specific kinds of URLs are checked for first. That is, the articles rewrite should be one of – if not the – last one in your whole htaccess. (That might be the case already. I can’t tell without seeing the htaccess.) So, try that, if it’s not already done. And if it is already done, then you’ll have to post your whole htaccess to us to review.

Also, an aside, when everything after “articles/” was a slug, it made sense to use FONT=Courier New[/FONT]. But now you’re trying to capture individual path segments, so you should probably be using [^/]+ instead.

True, but my website has “grown up” since last year, and now I’d like Article URL’s to look like this…


w[B][/B]ww.Debbie.com/management/other/postage-meters-can-save-you-money
w[B][/B]ww.Debbie.com/management/leadership/how-to-motivate-your-employees
w[B][/B]ww.Debbie.com/legal/general-counsel/why-retain-a-lawyer
w[B][/B]ww.Debbie.com/legal/intellectual-property/trademarking-your-company-name
w[B][/B]ww.Debbie.com/finance/tax-season/save-your-taxes-for-a-cpa
w[B][/B]ww.Debbie.com/finance/tax-season/how-to-avoid-filing-electronically
w[B][/B]ww.Debbie.com/finance/markets/jc-penney-said-to-hire-blackstone

That is already true.

[ot]What compounds this problem is that my website was 85% done last May when I had to relocate across the U.S. and started a new project which ended sooner than I thought, thus requiring me to move back across the U.S. this Fall and thus killing a lot of website momentum.

In May I completed re-engineered how my website is organized - think “Information Architecture” - and I came up with the new scheme of…


www.Debbie.com/{Section}/{Subsection}/{Article Slug}

Honestly, when I moved in my May, my “Article Section Landing Page” was broken, and that whole part of my website needs to be recoded and tweaked, although I believe the Article mod_rewrite above still applies?!
[/ot]

Not sure if I follow you on that one.


Here is where I think the problem is…

I always write my mod_rewrites in a very “loose” style, because I want my PHP to handle every possible URL and gracefully produce either a.) Redirect or b.) Error-Message

Earlier today, my PM URL was working when I just offered the ability to sort by column. Here is what I believe I had…


#PRETTY:	account/messages/incoming/by-sender
#UGLY:		account/messages.php?msgview=incoming&sortname=by-sender

#Rewrite only if the request is not pointing to a real file.
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule account/messages/((.+)/)?(.+)?$ account/messages.php?msgview=$2&sortname=$3 [L]

My code was able to handle all of these hacked URL’s…


h[B][/B]ttp://local.debbie/account/messages/incoming/by-date
h[B][/B]ttp://local.debbie/account/messages/incoming/by-d
h[B][/B]ttp://local.debbie/account/messages/incoming/
h[B][/B]ttp://local.debbie/account/messages/incoming
h[B][/B]ttp://local.debbie/account/messages/inco
h[B][/B]ttp://local.debbie/account/messages/

But where things start colliding with my Article mod_rewrite is when I changed things to this…


#PRETTY:	account/messages/incoming/by-sender/asc
#UGLY:		account/messages.php?msgview=incoming&sortname=by-sender&sortdir=asc

#Rewrite only if the request is not pointing to a real file.
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule account/messages/((.+)/)?((.+)/)?(.+)?$ account/messages.php?msgview=$2&sortname=$4&sortdir=$5 [L]

If I tried hacking the URL, some of the URL’s below were causing errors caused by my Article mod_rewrite?!


h[B][/B]ttp://local.debbie/account/messages/incoming/by-date/asc
h[B][/B]ttp://local.debbie/account/messages/incoming/by-date/as
h[B][/B]ttp://local.debbie/account/messages/incoming/by-date
h[B][/B]ttp://local.debbie/account/messages/incoming/by-d
h[B][/B]ttp://local.debbie/account/messages/incoming/
h[B][/B]ttp://local.debbie/account/messages/incoming
h[B][/B]ttp://local.debbie/account/messages/inco
h[B][/B]ttp://local.debbie/account/messages/

Again, here is my Article mod_rewrite… (This is almost the last mod_rewrite)


# SHOW ARTICLE
#PRETTY:	finance/tax-season/saves-your-taxes-for-a-cpa
#UGLY:		articles/article.php?section=finance&subsection=tax-season&article=saves-your-taxes-for-a-cpa

#Rewrite only if the request is not pointing to a real file.
RewriteCond %{REQUEST_FILENAME} !-f

#Match any kind of Section, Subsection and Article.  PHP will decide if it's valid or not.
RewriteRule (.+)/(.+)/(.+)$ articles/article.php?section=$1&subsection=$2&article=$3 [L]

Instead of trying to adjust my PM mod_rewrite, I am thinking I need to change my Article mod_rewrite so it basically says, "If you see a URL starting with /account or /account/messages then it doesn’t apply to the Article.

This may be more picky than others would be, but I want to be able to take the sample URL below, and start deleting character-by-character and my PHP code always handles things… (That is when I start getting issues with my Article mod_rewrite?!)


http://local.debbie/account/messages/incoming/by-date/asc

Hope that helps you out some… :-/

Sincerely,

Debbie