Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,092
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Conflicting mod_rewrites

    In another thread, I am trying to allow users to sort their Private Messages by Field in Asc/Desc order.

    Unfortunately, adding the extra parameter is causing a wicked collision with my Article mod_rewrite.

    Here is my not functioning mod_rewrite for PM's...
    Code:
    RewriteRule account/messages/((.+)/)?((.+)/)?(.+)?$ account/messages.php?msgview=$2&sortname=$4&sortdir=$5 [L]

    And here should be the universe of Private Message URLs...

    Private Message URLs
    Code:
    /account/messages/incoming/by-sender/asc
    /account/messages/incoming/by-sender/desc
    /account/messages/incoming/by-subject/asc
    /account/messages/incoming/by-subject/desc
    /account/messages/incoming/by-date/asc
    /account/messages/incoming/by-date/desc
    
    /account/messages/sent/by-recipient/asc
    /account/messages/sent/by-recipient/desc
    /account/messages/sent/by-subject/asc
    /account/messages/sent/by-subject/desc
    /account/messages/sent/by-date/asc
    /account/messages/sent/by-date/desc
    
    /account/messages/trash/by-sender/asc
    /account/messages/trash/by-sender/desc
    /account/messages/trash/by-subject/asc
    /account/messages/trash/by-subject/desc
    /account/messages/trash/by-date/asc
    /account/messages/trash/by-date/desc

    --------
    For my Articles, an Article has a Section and a Subsection.

    (There is no way to tell what Sections and Subsections there will be, other than the number will be large!!)

    Here is the Article mod_rewrite I used in the past...
    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]

    And here are some sample Articles...

    Sample Article URLs
    Code:
    /finance/tax-season/save-your-taxes-for-a-cpa
    /finance/tax-season/how-to-avoid-filing-electronically
    /finance/markets/jc-penney-said-to-hire-blackstone
    /legal/general-counsel/why-retain-a-lawyer
    /legal/intellectual-property/trademarking-your-company-name
    /management/other/postage-meters-can-save-you-money
    /management/leadership/how-to-motivate-your-employees

    I always write my mod_rewrites "loosely" and let my PHP handle every scenario. By that, I mean that a nefarious user should be able to whittle away at the URL (e.g. leave off a '/') and my PHP code should *gracefully* handle every scenario.

    Because my Article mod_rewrite is like one big wildcard, when the user deletes some of the PM URL like this...
    Code:
    /account/messages/incoming
    ...my mod_rewrites think that is the Section + Subsection + ArticleTitle for an Article, instead of a hacked PM URL?!


    I am thinking that maybe if I make my Article mod_rewrites more restrictive (e.g. Section cannot equal "account/") then that may solve a lot of the issues I started seeing tonight...

    Please help, because I'm dreadfully stuck on this one!!!

    Sincerely,


    Debbie

    P.S. Tango, if I can get this stuff fixed, then I'm all for using your PM sorting suggestions!

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,324
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    Daft question (I don'tuse mod-rewrite a lot) what are the .+ things about?

    I just use .* and it grabs whatever is there.. Oh and the question marks?

    I suspect one of the mods will move this into the apache forum shortly where I shall not follow!
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,324
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    Quote Originally Posted by doubledee View Post
    Here is my not functioning mod_rewrite for PM's...
    Code:
    RewriteRule account/messages/((.+)/)?((.+)/)?(.+)?$ account/messages.php?msgview=$2&sortname=$4&sortdir=$5 [L]
    Well seeing as nobody else chimed in..

    Shouldn't your rewrite rule be this?:

    Code:
    RewriteRule account/messages/((.+)/)?((.+)/)?(.+)?$ account/messages.php?msgview=$1&sortname=$2&sortdir=$3 [L]
    You're trying to use $2, $4 and $5 yet you only have 3 variables available from that pattern so you'll only have $1, $2 and $3 available.

    Well.. in theory anyway.. I don't like doing .htaccess stuff but thats what it looks like to me.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #4
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,092
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    Daft question (I don'tuse mod-rewrite a lot) what are the .+ things about?
    The . means "any character except new line" and the + means "one or more of..."


    Check this out...

    http://www.cheatography.com/davechil...r-expressions/


    Quote Originally Posted by tangoforce View Post
    I just use .* and it grabs whatever is there.. Oh and the question marks?
    ? means "zero or one"


    Quote Originally Posted by tangoforce View Post
    I suspect one of the mods will move this into the apache forum shortly where I shall not follow!
    Maybe you should...


    Debbie

  • #5
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,092
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    Well seeing as nobody else chimed in..

    Shouldn't your rewrite rule be this?:

    Code:
    RewriteRule account/messages/((.+)/)?((.+)/)?(.+)?$ account/messages.php?msgview=$1&sortname=$2&sortdir=$3 [L]
    You're trying to use $2, $4 and $5 yet you only have 3 variables available from that pattern so you'll only have $1, $2 and $3 available.

    Well.. in theory anyway..
    Wrong. I have nested Groups, so my numbers are right.


    Quote Originally Posted by tangoforce View Post
    I don't like doing .htaccess stuff but thats what it looks like to me.
    If you want to be any good at PHP and web development you need to master regex and mod_rewrites...


    Debbie

  • #6
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,300
    Thanks
    4
    Thanked 203 Times in 200 Posts
    Quote Originally Posted by doubledee View Post
    If you want to be any good at PHP and web development you need to master regex and mod_rewrites...
    I quite disagree with that assertion. There is a lot of talk out there about having "pretty" URLs but the indexing engines don't really care. What counts is content.
    Dave .... HostMonster for all of your hosting needs

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,324
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    Quote Originally Posted by doubledee View Post
    Wrong. I have nested Groups, so my numbers are right.
    Right.. well nice of you to reply like that considering I'm trying to help you. I did make it quite clear I was out of my depth and guessing. Always nice to have those you're trying to help poke at you..

    Quote Originally Posted by doubledee View Post
    If you want to be any good at PHP and web development you need to master regex and mod_rewrites...
    Absolutely charming . You are the one who is always asking for help yet you feel that you can belittle and judge other peoples coding abilities - even when they're helping you Telling people how YOU think they should be doing better when you're always wanting someone to guide you through every step isn't exactly polite.

    Really Deb, I thought you were past that
    Last edited by tangoforce; 11-12-2013 at 02:03 PM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This has nothing to do with PHP. Moving to the apache configuration forum.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #9
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,092
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    Right.. well nice of you to reply like that considering I'm trying to help you. I did make it quite clear I was out of my depth and guessing. Always nice to have those you're trying to help poke at you..
    Anyone ever tell you that you are WAY too sensitive??



    Quote Originally Posted by tangoforce View Post
    Absolutely charming . You are the one who is always asking for help yet you feel that you can belittle and judge other peoples coding abilities - even when they're helping you Telling people how YOU think they should be doing better when you're always wanting someone to guide you through every step isn't exactly polite.
    Learning mod_rewrites and regex is such a fundamental thing in programming for the web.

    That was my point.

    Whether you have a desire to learn those things is on you.


    Quote Originally Posted by tangoforce View Post
    Really Deb, I thought you were past that
    I wasn't belittling anyone. Just stating that it is very limiting to not know anything about Apache.

    Again, what you do or what motivates you is your choice.


    Debbie

  • #10
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,324
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    [QUOTE=doubledee;1371843]Anyone ever tell you that you are WAY too sensitive??[quote]

    Only those trying to excuse their own rudeness.

    Quote Originally Posted by doubledee View Post
    Learning mod_rewrites and regex is such a fundamental thing in programming for the web.
    To you Deb but then you're the one who can't get it working with your expertise. Lets remember this is fundamental so there is no excuse for you not knowing how to sort it

    Seriously I tried to help you, you got picky. There was no need.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #11
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,302
    Thanks
    28
    Thanked 276 Times in 270 Posts
    Quote Originally Posted by doubledee View Post
    Here is my not functioning mod_rewrite for PM's...
    Here's an attempt at a fix. I tested it using XAMPP and it worked. Didn't try it on my live server though.

    Code:
    rewriterule ^account/messages/(incoming|trash)/by-(sender|subject|date)/(asc|desc)/?$ /account/messages.php?msgview=$1&sortname=$2&sortdir=$3 [redirect=301,last]
    rewriterule ^account/messages/sent/by-(recipient|subject|date)/(asc|desc)/?$ /account/messages.php?msgview=sent&sortname=$1&sortdir=$2 [redirect=301,last]
    rewriterule ^account/messages/(incoming|trash)/by-(sender|subject|date)/?$ /account/messages.php?msgview=$1&sortname=$2 [redirect=301,last]
    rewriterule ^account/messages/sent/by-(recipient|subject|date)/?$ /account/messages.php?msgview=sent&sortname=$1 [redirect=301,last]
    rewriterule ^account/messages/(incoming|sent|trash)/?$ /account/messages.php?msgview=$1 [redirect=301,last]
    rewriterule ^account(?:/|/messages/?)?$ /account/messages.php [redirect=301,last]
    rewritecond %{REQUEST_FILENAME} !-f
    rewriterule ^([^/]+)/([^/]+)/([^/]+)/?$ /articles/article.php?section=$1&subsection=$2&article=$3 [redirect=301,last]
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #12
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,092
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Arbitrator,

    While it looks like your code would work, it is way too rigid.

    If nothing ever changed on my website, your solution might be okay. But the minute I start adding or changing PM Views or Sortable Fields, then all of your hard work would be out the window?!


    A better solution is one that is generic and scalable because it looks for *patterns*...


    Simply put, I need my mod_rewrite to be able to know that this URL is for an Article...
    Code:
    http://local.debbie/finance/accounting/postage-meters-can-save-you-money

    Whereas, this URL is for sorting Private Messages...
    Code:
    http://local.debbie/account/messages/incoming/by-subject/desc

    The problem is that the 1st URL has three wildcards:
    Code:
    Section + Subsection + Article
    And the 2nd URL also has three wildcards:
    Code:
    ViewType + SortField + SortOrder

    And my current mod_rewrites do not help Apache to see the difference between each scenario?!

    This confuses me to no end, because you would think that is pretty specific....
    Code:
    RewriteRule account/messages/((.+)/)?((.+)/)?(.+)?$ account/messages.php?msgview=$2&sortname=$4&sortdir=$5 [L]
    ...but Apache doesn't see it that way?!

    Sincerely,


    Debbie

  • #13
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,302
    Thanks
    28
    Thanked 276 Times in 270 Posts
    Quote Originally Posted by doubledee View Post
    This confuses me to no end, because you would think that is pretty specific....
    Code:
    RewriteRule account/messages/((.+)/)?((.+)/)?(.+)?$ account/messages.php?msgview=$2&sortname=$4&sortdir=$5 [L]
    ...but Apache doesn't see it that way?!
    . matches any character, including ampersands and slashes, if I'm not mistaken. I would use [^/&] instead. (I forgot to exclude ampersands in the code within my last post.)

    You should also be using non-capturing parentheses ((?: and )) where you have no intent to use the captured info. This will let you use sequential backreferences ($1, $2, and $3), makes it clear at a glance what info is and isn't used, and helps prevent accidental use of the wrong capturing group.

    Your code also has a logical issue with ((.+)/)?((.+)/)?(.+)? and requests like "account/messages/trash/by-sender" which will cause $2 and $5 to be assigned and result in unexpected behavior.

    This rule should also occur before the less specific article rule if it isn't already.

    Quote Originally Posted by doubledee View Post
    If nothing ever changed on my website, your solution might be okay. But the minute I start adding or changing PM Views or Sortable Fields, then all of your hard work would be out the window?!


    A better solution is one that is generic and scalable because it looks for *patterns*...
    You shouldn't be changing URLs that often anyway and rigid matches prevent accidental matches and expose errors, so I don't see the issue.

    Plus, your pattern, even when corrected, accepts invalid input; it matches URLs like "http://local.debbie/account/messages/invalid/by-sender/desc" and then returns URLs like "account/messages.php?msgview=invalid&sortname=by-sender&sortdir=desc". I don't think accepting generic URLs where specific parameter values are required is a good idea.

    Of course, you have the exact same problems with your articles redirection scheme; it's too generic. You can end up with three invalid parameters and, if a messages URL is accidentally mistyped by even one character, you can be redirected to an article URL instead.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #14
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,092
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Arbitrator,

    I tried your code and it removed my "Pretty URL" - which is the whole reason I'm using mod_rewrites?!


    Quote Originally Posted by Arbitrator View Post
    You should also be using non-capturing parentheses ((?: and )) where you have no intent to use the captured info. This will let you use sequential backreferences ($1, $2, and $3), makes it clear at a glance what info is and isn't used, and helps prevent accidental use of the wrong capturing group.
    I don't feel comfortable working with assertions, so I'm sticking with the basics for now.


    Quote Originally Posted by Arbitrator View Post
    Your code also has a logical issue with ((.+)/)?((.+)/)?(.+)? and requests like "account/messages/trash/by-sender" which will cause $2 and $5 to be assigned and result in unexpected behavior.
    Not sure what you mean here...


    Quote Originally Posted by Arbitrator View Post
    This rule should also occur before the less specific article rule if it isn't already.
    Done.


    Quote Originally Posted by Arbitrator View Post
    You shouldn't be changing URLs that often anyway and rigid matches prevent accidental matches and expose errors, so I don't see the issue.
    True, but I think most people would agree that you shouldn't be placing tons of logic into mod_rewrites.

    The whole concept of Regular Expressions is *patterns*.

    The way you did it, you are hard-coding everything.


    Quote Originally Posted by Arbitrator View Post
    Plus, your pattern, even when corrected, accepts invalid input; it matches URLs like "http://local.debbie/account/messages/invalid/by-sender/desc" and then returns URLs like "account/messages.php?msgview=invalid&sortname=by-sender&sortdir=desc". I don't think accepting generic URLs where specific parameter values are required is a good idea.

    Of course, you have the exact same problems with your articles redirection scheme; it's too generic. You can end up with three invalid parameters and, if a messages URL is accidentally mistyped by even one character, you can be redirected to an article URL instead.
    You're missing a key point.

    My mod_rewrites are coded "loosely" because my PHP does most of the logic checking.

    I use the mod_rewrite to take this...
    Code:
    account/profile/JohnDoe/about-me

    And create this...
    Code:
    account/profile.php?user=JohnDoe&tab=about-me

    It is not the mod_rewrite's job to determine if "JohnDoe" is valid, otherwise you get painted into a corner with unnecessary logic in your mod_rewrites like we seem to disagree on above...

    Sincerely,


    Debbie

  • #15
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,302
    Thanks
    28
    Thanked 276 Times in 270 Posts
    Quote Originally Posted by doubledee View Post
    I tried your code and it removed my "Pretty URL" - which is the whole reason I'm using mod_rewrites?!
    You may need to remove the redirect flags. That's the only way I could get it to work properly on an offline PHP installation (XAMPP). (I've never used .htaccess to redirect to a location that doesn't actually exist, but if your article rule is doing just that, I'd imagine that's the problem.)

    Quote Originally Posted by doubledee View Post
    Not sure what you mean here...
    I meant that if you supply two pseudo-directories in the URL and leave off the trailing slash, then the second and fifth capturing groups match since the fifth group is the only one that doesn't require a trailing slash.

    ((.+)/)?((.+)/)?(.+) with "account/messages/trash/by-sender" results in "account/messages.php?msgview=trash&sortname=&sortdir=by-sender" which is clearly wrong.

    Quote Originally Posted by doubledee View Post
    It is not the mod_rewrite's job to determine if "JohnDoe" is valid, otherwise you get painted into a corner with unnecessary logic in your mod_rewrites like we seem to disagree on above...
    I guess this is a valid design paradigm since you already need to design error handling into your PHP.

    My preference would be to never invoke PHP processing at all if the URL is invalid though; I'd either repair the bad URL, then send it to the server, or redirect to an error page.
    For every complex problem, there is an answer that is clear, simple, and wrong.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •