views:

79

answers:

4

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

www.site.com/character.php?id=3

I want the mod rewrite to change it to:

www.site.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.

Thanks ^_^

A: 

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.

Austin Fitzpatrick
A: 

Sounds like you might be able to use mod_rewrite rewritemap.

Maxwell Troy Milton King
+1  A: 

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 http://serverfault.com/

richsage
Hmm, could you provide me a little example in PHP, sorry to take up your time (use the first example please ) thanks :)
YouBook
Edited, see if that helps :-)
richsage
Weird, doesn't work though, I put down the connections and the right columns and the names.Could you provide a step by step, e.g. what goes in the htaccess (also, where htaccess must be), what goes in the PHP, what goes in the URL to make it work, like that. :-)
YouBook
Never mind, done it :-) problem I did was not changing in the htaccess with the character.php?id=$1 to what the php name is :-)
YouBook
Ok.. I have a problem again, in the URL it has a name for example,James_MayIt won't read it from the PHP since it has the _, I tried str_replacing it with the detection of strpos if the $_SERVER['REQUEST_URI'] has anything with a _ but still doesn't workHow do I do that?
YouBook
OK I've updated the post. No wonder you had issues, the slug doesn't come in via the POST array, it's a GET array. Teach me for replying late at night here ;-)
richsage
Oh of course, I changed the POST to GET :P I know about getting the variables by the URL that contains variables, however, you have not responded to my last request.QUOTE: "Ok.. I have a problem again, in the URL it has a name for example, James_May It won't read it from the PHP since it has the _, I tried str_replacing it with the detection of strpos if the $_SERVER['REQUEST_URI'] has anything with a _ but still doesn't work How do I do that?"
YouBook
I'm not sure why you're encountering that problem. What do you get for $slug immediately after getting it from the $_GET array? Is anything in the $_GET array at all?
richsage
Ah, figured it out, I used the str_replace to replace the _ with %20 no problem at all :)
YouBook
+1  A: 

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.

pixeline