SitePoint Sponsor

User Tag List

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

    mod_rewrite collisions

    I am in mod_rewrite *hell* right now...

    My website uses "pretty URL's" and everything has been working thanks to a lot of help in the past from Jeff Mott.

    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...
    Code:
    # 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...
    Code:
    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...
    Code:
    RewriteRule account/messages/((.+)/)?((.+)/)?(.+)?$ account/messages.php?msgview=$2&sortname=$4&sortdir=$5 [L]

    To back up a bit, I tried this one...
    Code:
    RewriteRule account/messages/((incoming|sent|trash)/)?((.+)/)?$ account/messages.php?msgview=$2&sortname=$4 [L]

    This URL works...
    Code:
    http://local.debbie/account/messages/incoming/by-date/

    But this one bleeds over into my Article mod_rewrite...
    Code:
    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...
    Code:
    http://local.debbie/account/messages/incoming/by-date

    Not sure if any of this makes any sense?!

    Sincerely,


    Debbie

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,311
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    #PRETTY: finance/tax-season/saves-your-taxes-for-a-cpa
    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 (.+). But now you're trying to capture individual path segments, so you should probably be using [^/]+ instead.
    "First make it work. Then make it better."

  3. #3
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,914
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    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.
    True, but my website has "grown up" since last year, and now I'd like Article URL's to look like this...
    Code:
    www.Debbie.com/management/other/postage-meters-can-save-you-money
    www.Debbie.com/management/leadership/how-to-motivate-your-employees
    www.Debbie.com/legal/general-counsel/why-retain-a-lawyer
    www.Debbie.com/legal/intellectual-property/trademarking-your-company-name
    www.Debbie.com/finance/tax-season/save-your-taxes-for-a-cpa
    www.Debbie.com/finance/tax-season/how-to-avoid-filing-electronically
    www.Debbie.com/finance/markets/jc-penney-said-to-hire-blackstone

    Quote Originally Posted by Jeff Mott View Post
    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.
    That is already true.

    Off Topic:

    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...
    Code:
    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?!



    Quote Originally Posted by Jeff Mott View Post
    Also, an aside, when everything after "articles/" was a slug, it made sense to use (.+). But now you're trying to capture individual path segments, so you should probably be using [^/]+ instead.
    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...
    Code:
    #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...
    Code:
    http://local.debbie/account/messages/incoming/by-date
    http://local.debbie/account/messages/incoming/by-d
    http://local.debbie/account/messages/incoming/
    http://local.debbie/account/messages/incoming
    http://local.debbie/account/messages/inco
    http://local.debbie/account/messages/

    But where things start colliding with my Article mod_rewrite is when I changed things to this...
    Code:
    #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?!
    Code:
    http://local.debbie/account/messages/incoming/by-date/asc
    http://local.debbie/account/messages/incoming/by-date/as
    http://local.debbie/account/messages/incoming/by-date
    http://local.debbie/account/messages/incoming/by-d
    http://local.debbie/account/messages/incoming/
    http://local.debbie/account/messages/incoming
    http://local.debbie/account/messages/inco
    http://local.debbie/account/messages/

    Again, here is my Article mod_rewrite... (This is almost the last mod_rewrite)
    Code:
    # 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?!)
    Code:
    http://local.debbie/account/messages/incoming/by-date/asc

    Hope that helps you out some...

    Sincerely,


    Debbie


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
  •