Home >Backend Development >PHP Tutorial >How Can I Create User-Friendly URLs in PHP?
Creating User-Friendly URLs in PHP
Traditional profile URLs often include user IDs in the query string, such as:
www.domain.com/profile.php?u=12345
However, modern websites prefer cleaner URLs like:
www.domain.com/profile/12345
To achieve this in PHP, one option is to utilize the .htaccess file with a mod_rewrite rule:
RewriteEngine on RewriteRule ^/profile/([0-9]+)\.html /profile.php?u=
This rule redirects requests from /profile/63.html to /profile.php?u=63, effectively mapping the clean URL to the underlying script.
Another approach involves using ForceType in the .htaccess file:
<Files news> ForceType application/x-httpd-php </Files>
This forces any request to the /news directory to be processed by PHP. Then, in index.php, you can access the requested path using $_SERVER['PATH_INFO'], which will contain the clean URL (e.g., /63.html).
The above is the detailed content of How Can I Create User-Friendly URLs in PHP?. For more information, please follow other related articles on the PHP Chinese website!