Bopo Posted May 6, 2009 Share Posted May 6, 2009 Hey Well I'm just about to start on a CMS, and I was wondering if this is possible by just using the .htaccess file: www.website.com/cars/viewarticle.php?id=123 To www.website.com/cars/ford-fiesta.php To generate the keywords, I would pull them from a column within the database, such as heading or title column, please could I get some opinions of whether something like this would work? Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/157085-re-writing-dynamic-url-to-keyword-friendly-url/ Share on other sites More sharing options...
gffg4574fghsDSGDGKJYM Posted May 8, 2009 Share Posted May 8, 2009 No you can't. Converting 123 to 'ford-fiesta' need access to a database which htaccess doesn't. You will have to query the database and find the id within your php with the name instead. You can make your URL like that : http://www.mysite.com/cars/test.html /.htaccess Options +FollowSymLinks <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^cars\/(.*)\.html$ /cars.php?page=$1 [NC,L] </IfModule> /cars.php <?php if (isset($_GET['page'])) { $page = $_GET['page']; } else { header("HTTP/1.0 404 Not Found"); header('Location: http://www.mysite.com/404.html'); die(); } require_once('databaseconnextion.php'); if (get_magic_quotes_gpc()) { $page = stripslashes($page); } $page = mysql_real_escape_string($page); $sql = "SELECT * FROM carslist WHERE name='".$page."';"; $result = mysql_query($sql); if (!$result) { die("SQL Error : ".$sql." -- ".mysql_error()); } if (mysql_num_rows() != 1) { header("HTTP/1.0 404 Not Found"); header('Location: http://www.mysite.com/404.html'); die(); } .... ?> I won't wrote the whole file but you get the idea. Hope it answer your question Quote Link to comment https://forums.phpfreaks.com/topic/157085-re-writing-dynamic-url-to-keyword-friendly-url/#findComment-829446 Share on other sites More sharing options...
JonnoTheDev Posted May 8, 2009 Share Posted May 8, 2009 No you can't. Of course you can if like you said you will use the title to create the url string. Also this is a bad rewrite rule: RewriteRule ^cars\/(.*)\.html$ /cars.php?page=$1 [NC,L] Using * allows anything through. What you may want to do is use an array map in your code so you can still do the lookup on the database primary key i.e 123 $map[123] = 'ford-fiesta'; $map[463] = 'ford-mondeo'; You can then get the array key from the url from the string to query the database. This is nothing new. Quote Link to comment https://forums.phpfreaks.com/topic/157085-re-writing-dynamic-url-to-keyword-friendly-url/#findComment-829493 Share on other sites More sharing options...
JonnoTheDev Posted May 8, 2009 Share Posted May 8, 2009 However it requires more code by just using the title and not using a key at all. It would be a bad idea to query the database on a varchar field with a string as the table may get large and using text is slow. There is nothing wrong with using the following as a url: http://www.website.com/cars/article/123/ford-fiesta This will get the keyword in the url to help SEO The .htaccess will look as follows: Options +Indexes RewriteEngine On Options +FollowSymLinks -MultiViews # product categories RewriteRule ^cars/article/([0-9]+)/[a-z0-9-]+$ /viewarticle.php?id=$1 [L] Quote Link to comment https://forums.phpfreaks.com/topic/157085-re-writing-dynamic-url-to-keyword-friendly-url/#findComment-829510 Share on other sites More sharing options...
gffg4574fghsDSGDGKJYM Posted May 8, 2009 Share Posted May 8, 2009 When i say no you can't. I mean you can't do it ONLY with .htaccess. You have to use php too. Your example aren't really usefull in the real world. I mean what the point in using a database if you need to manually write a php each time something change....You will have to add $map[786] = 'ford-something'; manually each time a new entry is made to the database or create another php file to do that, that not really usefull. Mine will work safely without anything else if you add something into the database. Using that : http://www.website.com/cars/article/123/ford-fiesta will be a bad idea since that : http://www.website.com/cars/article/123/sex-casino-viagra will be a perfeclty working url that can be indexed by search engine and will if they have enough backlink. Unless you made another query with the varchar in it, so there no point at all in using the number in the first place. If you are in a competitive field and you let a hole like that, some people will probably use it. Just create some account on popular website and link then back to this site, the amount of backlink may be enough for the bad version to be added to search engine instead of the one you want. Don't give anyone a chance to hurt you. Another reason to not use that : it may create duplicate content. Using a varchar in a SQL query isn't that bad with proper index it can be fine even on a hundred thousand query by day. And yes i have live working website that work that way. Quote Link to comment https://forums.phpfreaks.com/topic/157085-re-writing-dynamic-url-to-keyword-friendly-url/#findComment-829537 Share on other sites More sharing options...
Bopo Posted May 8, 2009 Author Share Posted May 8, 2009 Thanks for the great information, ill start working on it Quote Link to comment https://forums.phpfreaks.com/topic/157085-re-writing-dynamic-url-to-keyword-friendly-url/#findComment-829544 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.