php - URL rewriting and redirection (with database data) -


my url http://testurl.com/user.php?id=1&name=testuser

my default url above link need http://testurl.com/user/12345/testuser

if user try change in own browser link below

http://testurl.com/user/12345 http://testurl.com/user/12345/ http://testurl.com/user/12345/testusers_bla-bla 

then url bar automatically changes http://testurl.com/user/12345/testuser

edit

<?php $name = "this-is-a-test-user"; $id = 1;   $fields = array('id' => $id,                 'name' => $name);   $url = "http://localhost/view_user.php?" . http_build_query($fields, '', "&"); ?>  <a href = "<?php echo $url; ?>"> view user</a> 

you need 2 things here.

1) htaccess rules handle

  • http://testurl.com/user/12345
  • http://testurl.com/user/12345/
  • http://testurl.com/user/12345/xxx

and corresponding rules avoid duplicate content (redirect old format /user.php?xxx new format /user/id/name)

to so, can put code in root htaccess

options -multiviews  rewriteengine on rewritebase /  rewritecond %{the_request} \s/user\.php\?id=([0-9]+)\s [nc] rewriterule ^ user/%1? [r=301,l]  rewritecond %{the_request} \s/user\.php\?id=([0-9]+)&name=([^&\s]+)\s [nc] rewriterule ^ user/%1/%2? [r=301,l]  rewriterule ^user/([0-9]+)/?$ user.php?id=$1 [l] rewriterule ^user/([0-9]+)/([^/]+)$ user.php?id=$1&name=$2 [l] 

note: @ point, make sure mod_rewrite enabled , htaccess allowed (in apache configuration file).

a simple test: http://example.com/user/12345/xxxxx should internally rewrite /user.php?id=12345&name=xxxxx.

2) need adapt user.php logic (this here check data <id, name> pair existence)

<?php if (!isset($_get['id']) || empty($_get['id'])) {    // error page not found (since there no id)    header("http/1.1 404 not found");    return; }  if (!isset($_get['name']) || empty($_get['name'])) {    // no name -> id    $name = getnamebyid($_get['id']); // function checks in database     if ($name === null)    {       // error: id unknown -> page not found       header("http/1.1 404 not found");       return;    }     // id exists, have name -> redirect /user/id/name (instead of /user/id)    header("http/1.1 301 moved permanently");    header("location: /user/".$_get['id']."/".$name);    return; }  // if reach here, have id , name in url // have check if name corresponds id (and if id exists)  $name = getnamebyid($_get['id']); // function checks in database  if ($name === null) {    // error: id unknown -> page not found    header("http/1.1 404 not found");    return; }  // now, check if name in url corresponds 1 got database  if ($name !== $_get['name']) {    // doesn't -> redirect name    header("http/1.1 301 moved permanently");    header("location: /user/".$_get['id']."/".$name);    return; }  // finally, here we're fine. // have do...  ?> 

i intentionally write code "duplicate" parts let understand logic.
of course, can improve it.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -