htaccess mod_rewrite

I’m trying to put something with this, whenever I go to a page like:

http://www.example.com/character.php?id=3

I want the mod rewrite to change it to:

http://www.example.com/character/Jim_Carrey

Which of course, the ID is the row of the character name…

For that kind of example… I’ve tried to work with it, but don’t seem to get most of the production of htaccess because I haven’t worked with .htaccess a lot really.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

It’s actually the other way around:

  1. On your website, you write all your links the way you want them to look. For instance http://www.site.com/character/Jim_Carrey

  2. In your database, you add a field,
    commonly called “slug” or
    “post_slug” which refers to the
    Jim_Carrey” part of the url. IT MUST
    BE A UNIQUE ELEMENT, much in the way
    of a primary key. So make sure you
    have a function that does take care
    of creating the slug based on a
    given string (the post title for
    example) and making sure there is no
    duplicate.

  3. Then in your .htaccess (on the root folder) you do
    something like

    RewriteEngine On
    RewriteRule ^character/([a-z0-9_\-]+)/$ character.php?slug=$1 [L,NC]
    
  4. Finally, in your character.php
    script, you do a database query not
    against the ID, but against the
    post_slug field.

Solution 2

If you’re only using the character’s name, then something like the following would do

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

with a URL of eg http://www.example.com/character/Jim_Carrey. You’ll then need to look up the character’s name in the database using the slug passed in, as you won’t have the ID to look it up with.

Alternatively you could include the ID in the URL if you need it, vis:

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

This way you could have a URL like http://www.example.com/character/3/Jim_Carrey which would include the character name (for SEO reasons etc etc), but also the ID which you could then look up directly in your database.

Edit a small PHP example for you re the first one:

<?php

  // ... 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 helps! As always, use bound parameters where possible for your queries, and perform sanitization of your user data well. The PEAR MDB2 module has a nice page on how to do this here.

Edit 2 a quick and dirty setup 🙂

.htaccess file as follows:

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

  • A URL to match that would be http://www.example.com/character/Jim_Carrey – with the phrase “Jim_Carrey” appearing in your $_GET array as $_GET[“slug”]. NB apologies, wrote that PHP sleepy last night above so no wonder $_POST wouldn’t work as its a GET request 🙂 I’ve updated it now!

  • Finally you need to make sure that your host supports the use of .htaccess files. The setup of this is out of the scope of SO so any Apache configuration questions you’d be best asking over at https://serverfault.com/

Solution 3

I’m fairly certain you can’t do this through htaccess. You’ll need to do it in PHP, querying the database using the information from the url (?id=3) and then calling the Header Function using what you’ve learned from the database.

Solution 4

Sounds like you might be able to use mod_rewrite rewritemap.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply