Home >Backend Development >PHP Tutorial >How Can I Create User-Friendly URLs in PHP?

How Can I Create User-Friendly URLs in PHP?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-21 21:20:12538browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn