Home >Backend Development >PHP Tutorial >How to Implement URL Rewriting in PHP Using .htaccess and play.php?
To implement URL rewriting, you'll need to modify your:
1. Modify .htaccess File:
Add the following code to your .htaccess file:
RewriteEngine On RewriteRule ^videos/play/([a-zA-Z0-9_-]+)$ videos/play.php?title= [L] RewriteRule ^videos/play/(\d+)/([a-zA-Z0-9_-]+)$ videos/play.php?id= [L]
2. Modify play.php Script:
In your play.php script, handle the parameters as follows:
<?php if (isset($_GET['title'])) { // Redirect based on title } elseif (isset($_GET['id'])) { // Redirect based on ID } else { // Display error or default page } ?>
From an SEO perspective, both URL rewriting formats are relatively acceptable. However, the second format (with the ID) is preferred for tracking and analytics purposes. It allows you to easily track viewership for specific videos.
In your specific example, you can choose the following implementation:
This will allow you to redirect either based on the title or the numerical ID.
The above is the detailed content of How to Implement URL Rewriting in PHP Using .htaccess and play.php?. For more information, please follow other related articles on the PHP Chinese website!