CodeIgniter实现SEO友好博客链接求助:将?id=1形式改为自定义URL
Hey there! Let's get those clean, SEO-friendly URLs (like http://www.example.com/something-something) working in your CodeIgniter blog instead of the query string format. Here's a step-by-step guide tailored to your needs:
1. Enable URL Rewriting (Remove index.php)
First, we need to strip out index.php from your URLs and turn on Apache's mod_rewrite. Create or update a .htaccess file in your CodeIgniter root directory with this code:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]
Most hosting providers have mod_rewrite enabled by default, but if you hit issues, double-check that it's active on your server.
2. Add a Slug Field to Your Database
You'll need to store the SEO-friendly string (called a "slug") for each blog post. Alter your blogs table to add a unique slug column:
ALTER TABLE blogs ADD COLUMN slug VARCHAR(255) UNIQUE NOT NULL;
When creating new blog posts, generate a slug from the post title using CodeIgniter's built-in helper:
// In your blog creation controller $title = $this->input->post('title'); $slug = url_title($title, '-', TRUE); // Converts "My Awesome Post" to "my-awesome-post" // Optional: Avoid duplicate slugs by appending a number if needed $count = $this->db->where('slug', $slug)->count_all_results('blogs'); if ($count > 0) { $slug .= '-' . ($count + 1); } // Save the slug to the database alongside your post data
3. Configure CodeIgniter Routes
Tell CodeIgniter how to map slug URLs to your controller. Open application/config/routes.php and add a route rule based on your preferred URL structure:
Option 1: Direct slug URLs (e.g., example.com/my-awesome-post)
// Put specific page routes FIRST to avoid conflicts $route['home'] = 'pages/home'; $route['about'] = 'pages/about'; // Catch-all route for blog slugs (goes last!) $route['(:any)'] = 'blogs/view/$1';
Option 2: Slugs with a /blog/ prefix (e.g., example.com/blog/my-awesome-post)
$route['blog/(:any)'] = 'blogs/view/$1';
4. Update Your Blog Controller
Modify your blogs controller's view method to fetch posts by slug instead of ID:
public function view($slug) { // Fetch the post using the slug $data['post'] = $this->db->where('slug', $slug)->get('blogs')->row(); // Show 404 if no post matches the slug if (!$data['post']) { show_404(); } // Load your post view with the fetched data $this->load->view('blog/post', $data); }
5. Update Links in Your Views
Replace any old query-string links with the new slug-based URLs:
// Old link format echo anchor('blogs?id=' . $post->id, $post->title); // New link (matches your route choice) echo anchor($post->slug, $post->title); // OR for the /blog/ prefix: echo anchor('blog/' . $post->slug, $post->title);
That's all! Your blog should now use the clean, database-stored SEO URLs. If you run into 404 errors, double-check your route order, .htaccess file, and make sure slugs are being saved correctly to the database.
内容的提问来源于stack exchange,提问作者Pankaj

