Home > Article > Backend Development > How to Create Clean URLs with .htaccess?
Question:
How can we transform a URL like "http://localhost/index.php?user=1&action=update" into a cleaner version: "http://localhost/user/1/update"?
Answer:
To achieve this, you can modify your .htaccess file with the following code:
Options +FollowSymLinks RewriteEngine On RewriteRule ^user/([0-9]*)/([a-z]*)$ ./index.php?user=&action=
Here's how it works:
Accessing Parameters in PHP:
In your PHP code, you can access the URL parameters like this:
<code class="php"><?php echo "user id:" . $_GET['user']; echo "<br>action:" . $_GET['action']; ?></code>
This approach ensures more control and security compared to using a wildcard character (.*) in the RewriteRule, which could potentially match any string.
The above is the detailed content of How to Create Clean URLs with .htaccess?. For more information, please follow other related articles on the PHP Chinese website!