How can PHP be integrated with mod_rewrite for URL manipulation?
To integrate PHP with mod_rewrite for URL manipulation, you can use mod_rewrite to rewrite URLs to a specific PHP file that will handle the requests. This PHP file can then parse the rewritten URL and perform the necessary actions based on the URL parameters.
// .htaccess file to enable mod_rewrite
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
// index.php file to handle rewritten URLs
<?php
$url = $_GET['url'];
$urlParts = explode('/', $url);
// Example usage: localhost/myapp/user/123
if($urlParts[0] == 'user' && isset($urlParts[1])) {
$userId = $urlParts[1];
// Perform actions based on $userId
}
?>
Keywords
Related Questions
- What potential pitfalls should be considered when using header("Location: ...") in PHP for redirection purposes?
- How can PHP code be optimized to avoid overwriting variables in loops, as seen in the provided example?
- Are there any resources or examples available for integrating a stored procedure with PHP for web interface usage?