General mod rewrite question

Status
Not open for further replies.

LotsOfZeros

^^^ Bi-Winning ^^^
Feb 9, 2008
4,648
118
0
www.makemoniesonline.com
What is needed to create a keyword rich url via mod-rewrite? Can you use elements from your database for this?

Here's an example of my question. let's say I have a link that looks like this:
Code:
hxxp://www.mysite.com/record.php?id=12345
and when I mod-rewrite something like this I get:
Code:
hxxp://www.mysite.com/record/12345
In that example, all of the components of the mod-rewritten url are contained within the original dynamic URL but what if I wanted to have the new URL read more like this:
Code:
hxxp://www.mysite.com/honda/L7523-radiator-cap-replacement
As you can see, I have a car parts database where record ID#12345 actually contains a radiator cap replacement for a Honda, manufacturer's part number L7523.

That information was not contained in my original dynamic link but within my database itself.

Is doing something like this possible with mod-rewrite and how hard is it to accomplish?
 


It's possible but it has more to do with php than it does the mod rewrite. For example, you ever noticed all those wordpress urls that use only the article's title instead of an ID?

You can do it one of two ways, you can either pass everything off to PHP and let PHP parse the url for the appropiate information from the database, or you can pass the the parts (ie : ^(.*)/(.*)-(.*) product.php?mfr=$1&part=$2&desc=$3 or somthing like that).

Either way once you got the parts parsed and got the part number isolated you can do a query such as
"SELECT * FROM PARTS WHERE MFR_PART_NO = ".$part_no." LIMIT 1" and then produce a page based on the return result.

Since you have the mod_rewrite happening at the root level, and the brand/make could be anything, I'd recommend going the route of having PHP handle it all, rather than breaking it down in mod_rewrite, kinda like how wordpress does here.

Code:
Options +FollowSymLinks

RewriteEngine On
RewriteBase /

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Basically all requests gets thrown to index.php and decided from there.
 
You can definitely do what you want with mod rewrite. Just have to play with it. It's been several years since I've thought about it, so really fuzzy. But here is an example using an old recipe site.

This rule:
RewriteRule ^([0-9]+)-([a-z]+)\.html$ ../index.php?t=show&id=$1 [NC]

Finds this URL:
hxxp://www.sitename.com/recipe/399-beef.html

and rewrites it to:
index.php?t=show&id=399

A couple things to note in this example - the regular expression in the rewrite rule starts matching with the first number it finds. And since this match is happening one subdirectory below home, it used "../" to find index.php
 
hxxp://www.mysite.com/honda/12345-L7523-radiator-cap-replacement

Can be rewritten to hxxp://www.mysite.com/record.php?id=12345

with

RewriteRule ^([0-9]+)((-[a-z][0-9][A-Z])+)$ ../record.php?id=$1 [NC]

Knowing me, that rewrite rule is probably not quite correct, but I'm sure you can work with it. I've been drinking Captains and Diet so I don't feel like testing it at all right now. Best of luck.
 
Status
Not open for further replies.