URL redirection and redirection (with database data)

My url http://testurl.com/user.php?id=1&name=TestUser

My default url for the link above should be like http://testurl.com/user/12345/TestUser

If the user tries to change it in their own browser link, as shown below

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

then the url string will automatically change to 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>
+3
source share
3 answers

You need two things here.

1) htaccess rules to handle

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

and corresponding rules to avoid duplication of content (redirect the old format /user.php?xxxto the new format /user/ID/NAME)

To do this, you can put this code in your htaccess root directory

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]

: , mod_rewrite htaccess ( Apache).

: http://example.com/user/12345/XXXXX /user.php?id=12345&name=XXXXX.

2) user.php ( <ID, NAME>)

<?php
if (!isset($_GET['id']) || empty($_GET['id']))
{
   // error page not found (since there is no ID)
   header("HTTP/1.1 404 Not Found");
   return;
}

if (!isset($_GET['name']) || empty($_GET['name']))
{
   // no name -> get it by its ID
   $name = getNameByID($_GET['id']); // this function checks in the database

   if ($name === NULL)
   {
      // error: ID is unknown -> page not found
      header("HTTP/1.1 404 Not Found");
      return;
   }

   // ID exists, we now have its name -> redirect to /user/ID/NAME (instead of /user/ID)
   header("HTTP/1.1 301 Moved Permanently");
   header("Location: /user/".$_GET['id']."/".$name);
   return;
}

// if we reach here, we have an ID and a name in the url
// we have to check if NAME corresponds to ID (and if ID exists)

$name = getNameByID($_GET['id']); // this function checks in the database

if ($name === NULL)
{
   // error: ID is unknown -> page not found
   header("HTTP/1.1 404 Not Found");
   return;
}

// now, check if NAME in the url corresponds to the one we got from database

if ($name !== $_GET['name'])
{
   // it doesn't -> redirect to good NAME
   header("HTTP/1.1 301 Moved Permanently");
   header("Location: /user/".$_GET['id']."/".$name);
   return;
}

// finally, here we're fine.
// do what you then have to do...

?>

"" , .
, .

+1

root/.htaccess

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/([a-zA-Z0-9]+)/?$ /user.php?id=$1 [QSA,L]

RewriteRule ^user/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /user.php?id=$1&name=$2 [QSA,L]

, / .

+1

, .

, , htaccess:

RewriteEngine On

. :

 RewriteRule ^user/(\w+)/(\w+)$ user.php?id=$1&name=$2
+1
source

All Articles