SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RewriteRule and page number

    Hi guys!

    Is there any way to rewrite this rule so that it also supports a page parameter on the end of the URL (i.e. page=2).

    Rule i'm currently using:
    Code:
    RewriteRule browse-jobs/(.*)(/(.*)(/(.*)(/(.*))?)?)?$ directory.php?type=custom&url1=$1&url2=$3&url3=$5&url4=$7 [L]
    The current URL this works for is something like:

    mydomain.com/browse-jobs/uk/animal-charity-jobs/permanent/london

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)

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

    Guido's got one way to do it ([QSA] retains a preexisting query string) but I'm far more concerned over your (.*) series.

    Let me start with kudos for embedding the series of optional atoms (I had to look at it twice to be sure it's correct).

    However, you've been around here long enough to know that I have a series of rants and this was my Number 1:

    [rant #1]
    The use of "lazy regex," specifically the EVERYTHING atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is "greedy." Unless you provide an "exit" from your redirection, you will ALWAYS end up in a loop!
    [/rant #1]

    Okay, okay, your code will not loop but it won't protect you against some pretty weird URIs entered by accident (or by a savvy hacker). I have to hope that your directory.php script can handle bad entries for each of the atoms generated.

    Okay, I can't just whine about (.*) without providing an alternative: ([-a-z]+) will require AT LEAST ONE lowercase letter or hyphen to be contained in this atom.

    Your code will allow NOTHING (repeating /'s) to be matched. Does directory.php handle null values?

    Finally, the last atom created can also contain /'s and more subdirectories to the max allowed length of the URI. I doubt that you want to explain to directory.php how to handle that one!

    Okay, the reasons for my being pedantic are:

    1. Remind you and let other members know the misuse of (.*) should be restricted to the rare case of where you want to capture EVERYTHING (or NOTHING). Generally, this is merely duplicating the {REQUEST_URI} variable which is the smarter thing to do.

    2. Remind you and let other members know that the misuse of (.*) allows an easy path to hackers to access your database via the variables you're creating.

    3. Remind you and let other members know that the misuse of (.*) means that you've got to transfer the handling of inappropriate URIs to the file handler (directory.php in this instance) while it's far simpler to use mod_rewrite to prevent sending garbage to your handler - the ErrorDocument is a far more appropriate place for garbage URIs.

    Back to your question and guido2004's QSA recommendation, simply apply ?page=2 (or whatever page number you want to send along to directory.php) and replace the [L] with [QSA,L].

    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
  •