How to Remove Controller Name from URL in Codeigniter

calendar_month 17 Apr 2023 | Codeigniter


In this tutorial, you will learn how to remove controller name from url in codeigniter with example. CodeIgniter is a popular PHP framework for building web applications. One common issue that developers face while working with CodeIgniter is the controller name being displayed in the URL. This not only looks unprofessional but also hampers the SEO of your website. In this blog post, we will discuss how to remove the controller name from the URL in CodeIgniter and make your website SEO-friendly.

Step 1: Remove index.php from URL

Before we remove the controller name from the URL, we need to remove the index.php from the URL.

If you've already done that then that's a good thing, if not then visit: Remove "index.php" from url in Codeigniter 3 using htaccess

Step 2: Remove controller name from URL

Now that we have removed index.php from the URL, we can move on to removing the controller name from the URL. To do that, follow the below steps:

Open the routes.php file located in the application/config folder.

Look for the following code:

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

Add the following code below $route['default_controller'] = 'welcome';

$route['about'] = 'welcome/about';
$route['contact'] = 'welcome/contact';

Complete code:

$route['default_controller'] = 'welcome';
$route['about'] = 'welcome/about';
$route['contact'] = 'welcome/contact';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

This code will remove the controller name from the URL. So, if you have a URL like:
http://example.com/index.php/welcome/about
http://example.com/index.php/welcome/contact

it will be changed to

http://example.com/about
http://example.com/contact

Step 3: Update links in views
Now that we have removed the controller name from the URL, we need to update the links in our views. For example, if you have links like this:
<a href="index.php/welcome/about">About Us</a>
<a href="index.php/welcome/contact">Contact Us</a>

Change it to:

<a href="about">About Us</a>
<a href="contact">Contact Us</a>
This will ensure that all links on your website are pointing to the correct URLs.

Conclusion:
In this blog post, we have discussed how to remove the controller name from the URL in CodeIgniter. By following the above steps, you can make your website SEO-friendly and improve the user experience. Removing the controller name from the URL not only looks professional but also makes your website easier to navigate.

Buddy! I hope you relished the tutorial, and it was good to see you again. Keep learning. Keep visiting.

Related Tutorials
View all