How can PHP be used to create custom URLs for different pages on a website?
To create custom URLs for different pages on a website using PHP, you can use URL rewriting techniques. This involves rewriting the URL in a user-friendly format that masks the actual file structure on the server. This can be achieved by using the .htaccess file to redirect requests to a central PHP script that parses the URL and loads the appropriate page content dynamically.
// .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
// index.php file
$url = $_GET['url'];
switch ($url) {
case 'about':
include 'about.php';
break;
case 'contact':
include 'contact.php';
break;
default:
include 'home.php';
}
Keywords
Related Questions
- What are the best practices for handling user input validation and error handling in PHP scripts like the one discussed in the thread?
- What are common reasons for a PHP "absenden" button not functioning properly?
- What are the potential pitfalls of using single quotes around variables in PHP scripts?