Skip to main content
WebForum

Back to all posts

How to Enable and Configure Seo-friendly Urls in Codeigniter?

Published on
2 min read
How to Enable and Configure Seo-friendly Urls in Codeigniter? image

Creating SEO-friendly URLs is essential for the visibility of your website. In CodeIgniter, you can achieve this by removing the index.php from the URL and configuring your site accordingly. In this article, we will guide you through enabling and configuring SEO-friendly URLs in CodeIgniter.

Why SEO-Friendly URLs?

SEO-friendly URLs are crucial because they help search engines index your content more effectively. Clean URLs improve the user experience, making your content more accessible to visitors. Removing unnecessary parts of the URL, like index.php, creates a more meaningful and readable URL structure.

Step-by-Step Guide to Enable SEO-Friendly URLs in CodeIgniter

Step 1: Remove index.php from URLs

To remove index.php from your URLs, you need to modify your .htaccess file. This file uses Apache’s rewrite module to direct requests appropriately.

  1. Create or Edit the .htaccess File:

At the root of your CodeIgniter application (where your index.php file is located), create or edit a file named .htaccess with the following content:

RewriteEngine On RewriteBase /

Redirect to remove index.php

RewriteCond %{REQUEST_URI} ^system.* RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_URI} ^application.* RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?/$1 [L]

  1. Enable mod_rewrite:

Ensure that the mod_rewrite module is enabled in your Apache server. You can do this by running the following command:

sudo a2enmod rewrite sudo service apache2 restart

  1. Edit Configuration:

Open application/config/config.php and modify the following line to remove index.php:

$config['index_page'] = '';

Set the uri_protocol to:

$config['uri_protocol'] = 'REQUEST_URI';

Step 2: Use SEO Plugins for Additional Optimization

To further enhance your SEO efforts, consider using plugins that automate SEO tasks in CodeIgniter. You can find plugins to help with metadata, sitemaps, and more. Here are some resources to get you started:

Step 3: Test and Verify

After making these changes, test your URLs to ensure they’re working as expected. Access various parts of your site to confirm that the URLs are clean and functional.

Useful Resources

By following the steps outlined above, you can successfully enable and configure SEO-friendly URLs in your CodeIgniter application. This will ensure that your web application is better optimized for search engines, enhancing its visibility and user engagement.