I am attempting to put something with this particular, whenever I visit a page like:

world wide web.site.com/character.php?id=3

I would like the mod rewrite to alter it to:

world wide web.site.com/character/Jim_Carrey

Which obviously, the ID may be the row from the character title...

For your type of example... I have attempted to utilize it, try not to appear to obtain the majority of the manufacture of htaccess because I've not labored with htaccess a great deal really.

Thanks ^_^

It's really the other way round:

  1. In your website, you are writing all of your links how you would like them to appear. For example http://www.site.com/character/Jim_Carrey

  2. Inside your database, you give a area, generally known as "slug" or "publish_slug" which refers back to the Jim_Carrey" area of the url. It Has To Be Considered A UNIQUE ELEMENT, much in the manner of the primary key. So make certain you possess a function that takes care of making the slug with different given string (the publish title for example) and ensuring there's no duplicate.

  3. Then inside your .htaccess (around the root folder) you need to do something similar to

    RewriteEngine On
    RewriteRule ^character/([a-z0-9_\-]+)/$ character.php?slug=$1 [L,NC]
  4. Finally, inside your character.php script, you perform a database query not from the ID, but from the publish_slug area.

If you are only while using character's title, then something similar to the next would do

RewriteRule ^character/(.*)$ /character.php?slug=$1

having a Link to eg http://www.example.com/character/Jim_Carrey. You may then may need to look in the character's title within the database while using slug passed in, while you will not possess the ID to appear up with.

Alternatively training regimen the ID within the URL if you want it, vis:

RewriteRule ^character/([-9]+)/.*$ /character.php?id=$1

By doing this you might have a URL like http://www.example.com/character/3/Jim_Carrey which may range from the character title (for Search engine optimization reasons and so forth), but the ID for you to take a look up directly inside your database.

Edit a little PHP example for you personally re the first:


  // ... database connection stuff here etc

  $slug = $_GET["slug"];

  // IMPORTANT: perform some data sanitization here
  // I'm just going to make sure it's only letters, numbers and
  // hyphens/underscores as an example.
  $slug = preg_replace("/[^a-zA-Z0-9\-_]+/", "", $slug);

  // Now look up in your database
  // Ideally you'd have a slug column to compare this with, that you can fill
  // when your record is created/updated

  // You'd also be best off using bound parameters here, rather than directly
  // adding the data into the query string.  I personally use the MDB2 PEAR
  // module but feel free to use whatever you normally use.
  $qry  = "SELECT * FROM characters WHERE slug='" . $slug . "'";
  $result = mysql_query($qry, $db);

  // do something based on this result, fail if none found
  // or show details if OK etc
  // ...

Hope this can help! Of course, use bound parameters where feasible for your queries, and perform sanitization of the user data well. The PEAR MDB2 module includes a nice page regarding how to do that here.

Edit 2 a fast and dirty setup :-)

.htaccess file the following:

RewriteEngine On
RewriteRule ^character/(.*)$ /character.php?slug=$1
  • Your .htaccess file would ideally maintain the main of the site. Eg /home/wayne/public_html/ or wherever your index file is offered from

  • A Hyperlink to match that might be http://www.example.com/character/Jim_Carrey - using the phrase "Jim_Carrey" showing up inside your $_GET array as $_GET["slug"]. NB apologies, authored that PHP sleepy last evening above so no surprise $_Publish wouldn't act as its a GET request :-) I have up-to-date it now!

  • Finally you have to make certain that the host supports using .htaccess files. The setup of the has run out of the scope of SO so any Apache configuration questions you would be best asking at http://serverfault.com/

I am fairly certain you cannot do that through htaccess. You will need to get it done in PHP, querying the database while using information in the url (?id=3) after which calling the Header Function in what you've learned in the database.