Question : php query strings - mod rewrite with pretty URLS - db driven content

I have developed a CMS and the only thing lacking is pretty addresses.

the whole runs off of index.php and uses index.php?id=2 to get the content.

I would like to use a rewrite to make the links about.php, contact.php etc...

I think that can be easily accomplished, what I am worried about is if people link to them, if someone links to about.php instead of index.php?id=2  they will get a 404 error, how do I get around this?

Answer : php query strings - mod rewrite with pretty URLS - db driven content

To remove the index, you can just remove it from the rule.  

RewriteRule ^([a-z]+)\.html$ index.php?page=$1  [nc]

However, if you have product pages it may conflick, so you want to specify these pages like this:

RewriteRule ^([a-z]+)-P([0-9]+)\.html$ products.php?product=$1  [nc]

As for your "empty spaces and weird characters; please see the attached code.

Thank you!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
function create_url($id) {

// get your record from the database
mysql_select_db($connect, $dbconnect);
$query_Recordset_getcontent = "SELECT * FROM pages WHERE pages_menu_display = '$id' LIMIT 1";
$Recordset_getcontent = mysql_query($query_Recordset_getcontent, $dbconnect) or die(header("Location:problems.php"));
$row_Recordset_getcontent = mysql_fetch_assoc($Recordset_getcontent);
$totalRows_Recordset_getcontent = mysql_num_rows($Recordset_getcontent);

// put in new variable and trim whitespace from ends
$string = trim($row_Recordset_build_main_menu['pages_menu_display']);

// replace all dashes, underscores and spaces with just dashes
$string = preg_replace('#[-_ ]+#', '-', $string);

// make url
$url = "http://www.yourwebsite.com/$string.html";

// return url
return $url;

}

// here's how you may use the function
// (where 1 is the id of the link your creating
create_url(1);

// please let me know if you have any questions
Random Solutions  
 
programming4us programming4us