To implement URL rewriting in your PHP application, you can leverage mod_rewrite and the Kohana framework.
Step 1: Enable mod_rewrite
Ensure that the mod_rewrite module is enabled on your web server. Typically, this can be done by editing the .htaccess file in the web directory.
Step 2: Create .htaccess File for Kohana
Add the following code to a .htaccess file in your Kohana installation directory:
# Turn on URL rewriting RewriteEngine On # Installation directory RewriteBase /rootDir/ # Protect application and system files RewriteRule ^(application|modules|system) - [F,L] # Allow existing files to be displayed RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Rewrite remaining URLs to index.php RewriteRule .* index.php/<pre class="brush:php;toolbar:false"># Redirect play/google-io-2009-wave-intro RewriteRule ^videos/play/([a-zA-Z0-9-]+)$ /videos/play.php?title= [R] # Redirect play/203/google-io-2009-wave-intro RewriteRule ^videos/play/([0-9]+)/([a-zA-Z0-9-]+)$ /videos/play.php?id= [R]
Step 3: Redirect URLs
To redirect the specified URLs, add the following rules to your .htaccess file:
SEO-Friendly URL Choice
Regarding SEO, the second URL format, "http://example.com/videos/play/203/google-io-2009-wave-intro," is generally preferred due to its use of a numeric ID, which Google considers more informative. It also allows for numbered permalinks, facilitating the creation of chronological archives.
The above is the detailed content of How to Implement URL Rewriting in PHP using mod_rewrite and Kohana?. For more information, please follow other related articles on the PHP Chinese website!