PDA

View Full Version : Mod Rewrite Query


Ozwold
02-15-2005, 05:34 AM
Hi,

I've been reading this excellent thread on mod rewrite

http://forums.searchenginewatch.com/showthread.php?t=3925

and am trying to achieve something similar, I have the following URL:

http://www.domain.com/index.php?search=keyword

and would like to rewrite to:

http://www.domain.com/keyword/index.html

I've been having a go, but to no avail.

Thanks :)

skattabrain
02-15-2005, 10:43 AM
try this ....

RewriteEngine On
RewriteRule ^/([0-9a-zA-Z]+)/index.php /index.php?search=$1 [NC]

Ozwold
02-15-2005, 11:00 AM
Thanks skattabrain,

I'll give that a go.

:)

seomike
02-15-2005, 01:27 PM
The hard thing about using words instead of numbers is that you have to fill in the spaces. A query like this ?keyword=some%20keyword%20 can screw things up when sent through the mod rewrite.

I would suggest a function to add dashes into a keyword phrase and a function to parse them back out again.

Example:

<?
function static_url($phrase){
$phrase = strtolower($phrase);
$phrase = str_replace(" ","-",$phrase);
$phrase = str_replace("%20","-",$phrase);
return $phrase;
}

function keyword_phrase($phrase){
$phrase = str_replace("-"," ",$phrase)
$phrase = ucfirst($phrase);
return $phrase;
}
?>

Example usage for links on index.php the keyword being "blue widgets" coming in from a database.

<a href="<? echo static_url($db_record["name"]) ?>/index.html"><? echo $db_record["name"] ?></a>

this when parsed would look like this in the code
<a href="blue-widgets/index.html">Blue widgets</a>


Your rewrite rule would then pick up "blue-widgets" as the keyword instead of blue%20widgets.

now how to make the query to the db? assuming this is how yours is done.

"SELECT some_fields FROM some_table WHERE name LIKE '".keyword_phrase($keyword)."' OR name = '".keyword_phrase($keyword)."'"

This might not help at all but then again it might :)

Also your rewrite rule should be this

RewriteRule ^([^/]+)/index\.html$ /index.php?search=$1 [L]

skattabrain
02-15-2005, 01:35 PM
good one seomike, i'm doing this on one of my sites actually. nice catch.

Ozwold
02-15-2005, 01:41 PM
Thanks seomike,

Am I right saying that the final mod rewrite code should like this?

RewriteEngine On
RewriteRule ^/([0-9a-zA-Z]+)/index.php /index.php?search=$1 [NC]
RewriteRule ^([^/]+)/index\.html$ /index.php?search=$1 [L]

Also thanks for the top tip, I didn' think of that....

Thanks :)

seomike
02-15-2005, 05:49 PM
Here is what your final htaccess file should look like

RewriteEngine On
RewriteBase /

RewriteRule ^([^/]+)/index\.html$ /index.php?search=$1 [L]

You're all set now! If you have any questions just ask away! :)