SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Mod Rewrites

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Mod Rewrites

    Well its been about 3 years since I worked on apache rewrites, and it sure shows. My web host is horrible and I have jsut had enough, so I want to move everything to a new host. The problem is, the site doesnt work on the new host. I really dont know why, but it seems as though the variables are not carrying over in the mod rewrites. Im sure my code is atrociously riddled with error, but I dont know why it worked before and now it doesnt. Here is what I have narrowed it down to:

    ===
    Options +FollowSymLinks
    RewriteEngine on


    RewriteRule (.*)/([A-Za-z]*[u])/$ /details.php\?z\=$1\&w\=$2

    RewriteRule (.*)/([A-Za-z]*[u])$ /details.php\?z\=$1\&w\=$2
    RewriteRule page(.*)/cost(.*)-(.*)/(.*)$ index\.php\?curpage\=$1\&p\=$2\&costy\=$3\&t\=$4
    ===

    So that is the rewite that worrks on the old server, but not on the new. I have even tried such things as thus:

    ====
    RewriteRule (.*)/([A-Za-z]*[u])/$ /details.php\?z\=pleaseReturnaVariable
    ===
    And still it will not return $z. I have tried countless escape character variations as well. It does however send me to the correct page (details.php), but no variable is being sent over. I spent about 3 hours on this and still no luck. I really want to get out of this web host before they "transition" and break things again.

    Thanks for the help.

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    oc,

    First, welcome to SitePoint's Apache forum!

    Second, ARGH! You need to learn regex 'cause the biggest problem with mod_rewrite seen in this forum is the inappropriate use of (.*). Please have a read of my signature's linked tutorial Article and see if that doesn't help. (Hint: You do NOT escape characters in a redirection.)

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, Ill read it tonight. The escape characters look funny to me too, but it has been 3 years since I've looked at that code. I dont know why i used it, I assume it has something to do with php but who knows. I will say that it works on my old system but I guess that doesnt matter now. I will try to use something more up-to-date.

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    oc,

    I hope you also learned NOT to use (.*) unless you mean to capture EVERYTHING (including JUNK) and KNOW how to avoid looping (it'll capture the redirection, too, the way most newbies use it).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah I think I DO want to catch everything before the slashes, so as far as I can tell the expression is viable (note that I did not say efficient or perfect.) The problem is that the variable is not being passed. I am trying to figure out why this is and I just have no clue. My guess is that these rewites require the use of globals and that maybe the new host has them off or something.

    Is this how rewrites work? I mean, in essence I am just sending a variable via the address bar, correct? How then could one use rewrites without the use of global variables?

    I really hope that made sense; I have a dated and thus dilapidated understanding of programming. My expertise is in writing, but certainly not in this form.

    Again, thanks for the help. I would hate to call one of my programmer buddies over to share the headache. I'm glad I have someone to talk to that is an expert in this area.

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    oc,

    Okay, let's take a look at your code:
    Code:
    RewriteEngine on
    # good!
    
    RewriteRule (.*)/([A-Za-z]*[u])/$ /details.php\?z\=$1\&w\=$2 [L]
    # That says to CAPTURE everything before a /,
    # then CAPTURE zero or more uppercase or lowercase letters
    # followed by a u (the square brackets aren't needed)
    # and a / to terminate the string - too weird for me!
    
    # The redirection does not use the atom created
    # by the regex and should NOT have the \'s
    
    # The [L] was added to TERMINATE the block statement
    # so it's not ANDed with the next block statement
    
    RewriteRule (.*)/([A-Za-z]*[u])$ /details.php\?z\=$1\&w\=$2
    RewriteRule page(.*)/cost(.*)-(.*)/(.*)$ index\.php\?curpage\=$1\&p\=$2\&costy\=$3\&t\=$4
    # ARGH!  The same thing as above but then ANDed with
    # another RewriteRule which must match page, ANYTHING/NOTHING,
    # a /cost, another ANYTHING/NOTHING, a dash, a third
    # ANYTHING/NOTHING, another / then ANYTHING/NOTHING and
    # redirect to ... it doesn't matter 'cause it'll NEVER match after
    # the prior redirection.
    
    RewriteRule (.*)/([A-Za-z]*[u])/$ /details.php\?z\=pleaseReturnaVariable
    # Ditto the first explanation including the \'s in the redirection
    # and the lack of definition of pleaseReturnaVariable
    I suspect that your understanding of regex is what's at fault (the reliance on (.*) gives that away!) but, the [u] makes me think that you want to have '[u]' IN your URL - it won't happen! Have a look at http://www.ietf.org/rfc/rfc2396.txt which will show you which characters are allowed in a URL and under what conditions.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


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
  •