Php重写动态页面[关闭]

I have a page called profile.php?id=21 and I would like the path to be of my choosing from a text string stored in a database. So, for instance if I stored "my-page" in the database associated with this page I would like the full path to be "www.domainname.com/my-page/21" not "www.domainname.com/profile.php?id=21".

I am using a MySQL database. Any help would be appreciated.

This may help you, add it to the .htacess file

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/$ /profile.php?id=$2 [L]

Then any of these will call domainname.com/profile.php?id=21

domainname.com/my-page/21/
domainname.com/anything/21/
domainname.com/justnotaslash/21/

Just to expand on Andy Gee's answer above, you need to create a .htaccess file in the base directory of your website. (This is how projects like Drupal (drupal.org) and FlightPath (getflightpath.com) do it).

Let's say all traffic needs to go through index.php, and you plan to have a pseudo path at index.php?q=some/path

So you want site.com/index.php?q=some/other/path to look like this in the user's browser: site.com/some/other/path

Here is what your .htaccess file would need to contain (taken from the FlightPath .htaccess file, which is greatly influenced by the Drupal 6 .htaccess file):

<IfModule mod_rewrite.c>
  RewriteEngine on

#######################################
######################################  

  # Modify the RewriteBase if you are using FlightPath in a subdirectory or in a
  # VirtualDocumentRoot and the rewrite rules are not working properly.
  # For example if your site is at http://example.com/flightpath uncomment and
  # modify the following line:
  # RewriteBase /flightpath
  #
  # If your site is running in a VirtualDocumentRoot at http://example.com/,
  # uncomment the following line:
  # RewriteBase /

  # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>

After this, you would need to program index.php (or whatever you want to use) to look at $_GET["q"] for the pseudo path, and query the database and display content however you see fit.

BTW If you put this .htaccess file in place, and start getting weird server errors, just delete or rename the .htaccess file to return to normal, as it may mean that your webserver isn't set up to handle URL rewrite rules like this.