Home > Article > Backend Development > How can I create user-friendly URLs using .htaccess?
Creating Pretty URLs with .htaccess
Question: How can I transform URLs with multiple parameters into clean, readable formats using .htaccess?
Answer:
To create pretty URLs with .htaccess, you can utilize rewrite rules. Here's how:
Rewrite Rule for Single Parameter:
To transform URLs like http://localhost/index.php?user=1 into http://localhost/user/1 using .htaccess, you can implement the following:
<code class="htaccess">Options +FollowSymLinks RewriteEngine On RewriteRule ^user/(.*)$ ./index.php?user=</code>
Rewrite Rule for Multiple Parameters:
To convert URLs like http://www.yourwebsite.com/index.php?user=1&action=update into http://www.yourwebsite.com/user/1/update:
<code class="htaccess">Options +FollowSymLinks RewriteEngine On RewriteRule ^user/([0-9]*)/([a-z]*)$ ./index.php?user=&action=</code>
Accessing Parameters in PHP:
In PHP, you can retrieve the captured parameters:
<code class="php"><?php echo "user id:" . $_GET['user']; echo "<br>action:" . $_GET['action']; ?></code>
Additional Considerations:
The above is the detailed content of How can I create user-friendly URLs using .htaccess?. For more information, please follow other related articles on the PHP Chinese website!