Home >Backend Development >PHP Tutorial >How Can I Create User-Friendly URLs with .htaccess and Rewrite Rules?
Creating Friendly URLs with .htaccess
Introduction
Establishing user-friendly URLs is crucial for website usability and search engine optimization. .htaccess offers a versatile tool for achieving this by rewriting URLs into a more aesthetically pleasing and intuitive format. This article addresses a common challenge with .htaccess, namely, converting complex URLs into simpler ones while also incorporating additional parameters based on user input.
The Challenge
The provided sample URL demonstrates the need to convert complex URLs like "http://website.com/index.php?ctrl=pelicula&id=0221889" into more concise and user-friendly versions such as "http://website.com/pelicula/0221889/". Additionally, there is a requirement to include the article title at the end of the URL.
Solution
To craft friendly URLs with .htaccess, we can leverage rewrite rules. Here's a breakdown of how it works:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?url= [QSA,L]
$path_components = explode('/', $_GET['url']); $ctrl=$path_components[0]; $id=$path_components[1]; $tab=$path_components[2];
Additional Considerations
The above is the detailed content of How Can I Create User-Friendly URLs with .htaccess and Rewrite Rules?. For more information, please follow other related articles on the PHP Chinese website!