I want to use something like the following, but it returns an error when I try:
RewriteRule browse-jobs/(.*)/(.*)/(.*)/(.*) directory.php?type=custom®ion=$1&area=$2&job_type=$3&industry=$4 [NC,L]
My problem is that the 2nd, 3rd and 4th part might not always have values. However, the first part will always be matched.
Any ideas how to make it work?
Yes, a series of optional atoms each embedded in the previous one.
I’ve got to dash now but will check back in a few hours.
RewriteRule browse-jobs/(.*)(/(.*)(/(.*)(/(.*))?)?)?$ directory.php?type=custom®ion=$1&area=$3&job_type=$5&industry=$7 [L]
Making each successively embedded atom optional is similar to optional arguments in PHP function definitions (except for the regex, of course). With this approach, region is a requirement then, to have job_type, you must have an area and to have industry, you must have a job_type.
Now that I’ve gone through all that for you, I believe:
(.*) should be replaced by ([a-zA-Z]+) in each case (your keys don’t appear to contain digital values) and
this may not be what you’re after as you may need an industry but not a job_type, you may need a job_type but not a region, etc.
The problem with your optional key-value pairings is that YOU are in control of the links which get handled by this mod_rewrite so you need to “fill any nulls” with something like an _ (anything your directory.php script can handle and treat as a null value). If this is the case, add a _ after the Z (in comment #1) and you’ll be set.