Jump to content

Re-writing Dynamic URL to keyword friendly URL


Bopo

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.