Your rules are colliding because you didn’t set the L flag on your first rule. The L flag makes that rule the last to be processed, and if you don’t set it, then it just skips right down and does the next one. Also, if you are overwriting the query string then you don’t need QSA since that appends your new querystring on to the original one. You might be getting a 500 error because you need separate RewriteCond for each rule (at least I can’t get it to work with just one set).
Here is a working example:
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^locator/dealers/([^/]*)/?(.*)?$ locator/dealers.html?s=$1&c=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
’locator’ and ’dealers’ are pages in modx (parent and child respectivly) and I want locator/dealers/ne/lancaster to go to the page that pulls up dealers for Lancaster county in Nebraska. However, I don’t want to make MODx pages for this because I have a database table of counties that I’m using.
This could be accomplished by calling the dealers pages like this:
locator/dealers?county=lancaster&state=ne
but I want a friendly url.
So in order to make a working friendly url like this: locator/dealers/ne/lancaster I need to place a rewrite rule before the rule for modx and make sure it is set to be the last rule processed. The above code accomplishes that. It also makes sure that urls like locator/dealers/ne and locator/dealers/ne/ work as well since the second slash and remainder of the url are optional.
I’m using a snippet on the dealers.html page to grab the $_GET variables ’s’ and ’c’ and using them to find my county in the database and any pages associated with that county. A really cool thing to do on these custom database driven pages is to reset the pagetitle variable to be something unique. This makes these pages appear as unique as if you made them in MODx. Here is the code to do that as a plugin.
if (isset($_GET['s']) && isset($_GET['c'])) {
$county = str_replace('-',' ',ucfirst($_GET['c']));
$state = strtoupper($_GET['s']);
$query = "SELECT * FROM counties WHERE state = '$state' AND county = '$county'";
$result = $modx->db->query($query);
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_assoc($result)) {
$id = $row['id'];
$state = $row['state'];
$county = $row['county'];
$modx->documentObject['pagetitle'] = "Available Dealers in $county County, $state";
}
}
}
For the System Event choose: ’OnLoadWebDocument’ and give it any name you want. Note that I check the database to see if the county and state actually exist in the database before setting the pagetitle. This is to prevent someone having fun on my page and injecting whatever they want for a pagetitle. That might sound harmless until those pages get into Google.
Hope this helps.
James