Home > Backend Development > PHP Tutorial > How to Implement URL Rewriting in PHP using mod_rewrite and Kohana?

How to Implement URL Rewriting in PHP using mod_rewrite and Kohana?

Barbara Streisand
Release: 2024-12-09 21:27:10
Original
882 people have browsed it

How to Implement URL Rewriting in PHP using mod_rewrite and Kohana?

URL Rewriting in PHP with mod_rewrite and Kohana

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]
Copy after login
[PT,L]

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template