tags:

views:

89

answers:

3

Hey,

I want to change from this address:

www.domain.com/game.php?id=1

to somthing like that:

www.domain.com/game/1/THE-NAME-OF-THE-GAME-IN-DB

How can I do it?

And one more question:

I want when the user goes to address which doesnt exists, for example if it doesnt exists:

www.domain.com/game.php?id=9123912 - this ID doesnt exists

The server will pass him to 404 error. How can I do it?

Thank you very much.

EDIT:

game.php:

if (isset($_GET['id'])) {

    $row = mysql_num_rows(mysql_query("SELECT * FROM games WHERE id = '{$_GET['id']}'"));

    if ($row) {
        $game = mysql_fetch_array(mysql_query("SELECT * FROM games WHERE id = '{$_GET['id']}'"));
        Header( "HTTP/1.1 301 Moved Permanently" ); 
        Header( "Location: /game/".$game['id']."/".seo_title($game['name'])); 
    } 

    else {
        Header( "HTTP/1.1 404 Not found" ); 
    }

    exit;
}

htaccess:

LIKE YOU SAID. I JUST COPIED.
A: 

.htaccess isn't meant to perform url rewriting like you mention. If your server is running apache try this. If your server is running IIS try this.

As for the second question, if the id doesn't exist, have game.php perform a redirect to a pagename you know does not exist (like 9123912.html). The server should be configured to deliver the 404 natively then.

Joel Etherton
I'm pretty sure what he wants is *exactly* what URL rewriting is commonly used for - he's just stated it backwards.
Michael Borgwardt
+4  A: 

.htaccess

RewriteEngine On
RewriteRule ^game/([0-9]+)/ /game.php?newid=$1

game.php

if (isset($_GET['id'])) {
  $row = dbgetrow("SELECT * FROM games WHERE id = %s",$_GET['id']);
  if ($row) {
    Header( "HTTP/1.1 301 Moved Permanently" ); 
    Header( "Location: /game/".$row['id']."/".seo_title($row['name'])); 
  } else {
    Header( "HTTP/1.1 404 Not found" ); 
  }
  exit;
}

if (isset($_GET['newid'])) $_GET['id'] = $_GET['newid'];

this is a code, that works for me but considered to be a pseudo-code to give you an idea.
dbgetrow() function to get a row from the database
and seo_title() function could be like this one

function seo_title($s) {
    $s = strtolower(trim($s));
    $s = str_replace(" ","-",$s);
    $s = preg_replace('![^a-z0-9-]!',"",$s);
    $s = preg_replace('!\-+!',"-",$s);
    return $s;
}

The whole concept has been pinched from SO :)
the only games/1/ part is really important while the rest can be anything, game title is just for seo purpose. Take this question title for example:
http://stackoverflow.com/questions/3439430/not-really-a-question-title-lol
:)

Col. Shrapnel
Hey, first - thank you!It passes me to - www.domain.com/game// - why?and what is this $newid?thank you.
Luis
+ the second "if" why?
Luis
@Luis this is a code, that works for me but considered to be a pseudo-code to give you an idea. substitute a `dbgetrow()` function with database lookup and actual variables with your own field names. $newid for not to mess new seo links with old ones. onse your script gets $_GET['newid'] that means it's called with new link. and once with $_GET['id'] - it was called with old game.php?id=1 link and should be rewritten, with proper new address, `/game/1/THE-NAME-OF-THE-GAME-IN-DB` one. To make it, you have to fill an address template with data from DB
Col. Shrapnel
and possible seo_totle() function contents I've posted recently http://stackoverflow.com/questions/3437537/php-question-about-adding-hyphens-to-words/3437692#3437692
Col. Shrapnel
I changed the functions.. of course. Im working with PHP.The part of $row works.. the part of seo_title(), which function it is?
Luis
@Luis added it to answer.
Col. Shrapnel
Hmm.. again, its pass me to: www.domain.com/game//I am editing the question and I show you how I did it.
Luis
A: 

hi Luis,

i suggest very gud tool for error solving

htaccessredirect.net

enjoy if help u !!!

(spammy tagline deleted)

Please read the following: http://meta.stackoverflow.com/questions/57497/limit-to-self-promotion-in-answers
Will