Home >Backend Development >PHP Tutorial >How to Implement URL Rewriting in PHP Using .htaccess and play.php?

How to Implement URL Rewriting in PHP Using .htaccess and play.php?

Susan Sarandon
Susan SarandonOriginal
2024-12-06 07:28:10336browse

How to Implement URL Rewriting in PHP Using .htaccess and play.php?

URL Rewriting in PHP

Step-by-Step Implementation

To implement URL rewriting, you'll need to modify your:

  1. .htaccess file (in the root of your web directory)
  2. play.php script

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
}
?>

SEO Considerations

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.

Implementation Choices

In your specific example, you can choose the following implementation:

  • http://example.com/videos/play.php?title=google-io-2009-wave-intro
  • http://example.com/videos/play.php?id=203

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn