URL rewriting and redirection (with database data)

2019-05-18 04:40发布

问题:

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

My default url for above link would need to be like http://testurl.com/user/12345/TestUser

If user try to change it in own browser link like below

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

then url bar automatically changes 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>

回答1:

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 duplicate content (redirect old format /user.php?xxx to new format /user/ID/NAME)

To do so, you can put this code in your 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: at this point, make sure mod_rewrite is enabled and htaccess allowed (in Apache configuration file).

A simple test: http://example.com/user/12345/XXXXX should internally rewrite to /user.php?id=12345&name=XXXXX.

2) Now you need to adapt user.php logic (this is here you check for your data <ID, NAME> pair existence)

<?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...

?>

I intentionally write this code with "duplicate" parts to let you understand the logic.
Of course, you can improve it.



回答2:

Why not try to get a very basic rule working, then go from there.

First you need to make sure you server has the rewrite module enabled, then at the top of your htaccess file put:

RewriteEngine On

Then you can put your re-write rules using regex. Eg:

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


回答3:

Try this in root/.htaccess file

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]

The conditions make sure you don't rewrite any existing file/directories on the server.