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:


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

http://httpd.apache.org/docs/current/rewrite/flags.html

Check out the QSA flag

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). :tup:

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][indent]The use of “lazy regex,” specifically the :kaioken: EVERYTHING :kaioken: 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![/indent][/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