如何在PHP和Apache中使用友好URL [关闭]

I'm trying to get friendly URLs on my web site so I don't have to call specific file through URL.

Example:

Index page - http://www.website.com

Go to a users profile page - http://www.website.com/username

Could someone please point me in the right direction to find out how this is done.

Thank you.

You can use Apache rewrite engine module.

  1. Create file called .htaccess
  2. Add your rewrite condition in it

       <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
        Options -Indexes     </IfModule>
    

And all your requests to server will be forwarded to index.php and data from url you can get from Query String

$_GET["url"]

then you can do what ever you want with it.