tags:

views:

42

answers:

2

Can I rewrite a url by using the value 6 from ?cat for example, and checking my MySQL database for the value 6 category name html using PHP & MySQL if so how?

Current url.

http://localhost/index.php?cat=6&sub1=8&sub2=24&sub3=81

New search friendly URL displayed in browser.

http://localhost/html/basics/forms/select-tag/
A: 

Check out mod_rewrite.

You need to open up the .htaccess file(if it does not exist, create it) and include the following lines. Note that RewriteEngine On should be only written once.

RewriteEngine On # Turn on the rewriting engine 
RewriteRule ^html/basics/forms/select-tag/([0-9]+)/?$ index.php?cat=$1 [NC,L] 
SteD
A: 

Yes. Basically you will want to pass the entire url through a router script using mod_rewrite. The .htaccess file should have something like this:

RewriteEngine On
RewriteBase /    

#if it's an existing directory
RewriteCond %{REQUEST_FILENAME} -d [OR] 
#...or an existing file
RewriteCond %{REQUEST_FILENAME} -f 
#serve it up
RewriteRule ^(.+) - [PT,L] 

#else, direct to index.php
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

Then, in index.php you can have something like this:

$request = explode('/', $_GET['url']);

Which would have all your clean url segments in the $request array. Once you have those values, you can connect to the database, find what the url represents, and output the page. If it's not found, you can send a 404 header with a "Page not found" message.

header('HTTP/1.0 404 Not Found');
echo '<p>Page not found</p>';

So that's the basic clean url technique. If the url is the earlier, messy one, you would just add some logic to check for that, and redirect to the correct clean url:

if(isset($_GET['cat'])){
    //put some logic here

    header("Location: http://localhost/".$the_clean_url);
}

This basic technique should solve your problem with some tinkering.

handsofaten