Custom URL Routing in CodeIgniter: Step-by-Step Guide

Custom URL Routing in CodeIgniter: Step-by-Step Guide
Promote Your Content FREE!

Promote Your Contents FREE!

Promote your contents FREE, contents such as WebSites, YouTube channels, YouTube videos, Facebook, Instagram, LinkedIn, Twitter, Pinterest and many more.

Short Description

Learn how to do routing in CodeIgniter! Discover step-by-step methods to create SEO-friendly custom URLs using routes.php file.

Click to Visit

Opening content automatically in secounds...

Share

Or

Category: Science & Technology
Type: YouTube

Content Details

This content was added on Spotenjin at 12 Feb 2022 and got 1214 visits untill now.

Other Contents

SS Tech & Creation MG Tailor Upcoming new Tesla Vehicles Latest offers on Amazon How to do connection in pgsql database through php How to install wamp server on windows 10 | step by step wamp server installation guide Shahrukh Shaikh bowling multiple spin variation in cricket SS Tech and Creation Instagram How to do connection to mysql database through php How to remove index.php from url in codeigniter What is SQL Injection in Hindi SQL Injection attack in [English] IFB Diva Aqua VX 6kg 800RPM Washing Machine Unboxing How to install codeigniter on wamp | Structure of codeigniter How to Create Controller in CodeIgniter: Full Step Guide How to Create Database, Table & Insert Data in phpMyAdmin How to crack php interview for fresher | common php interview questions ask for fresher How to prevent SQL Injection in PHP in (English) How to make YouTube videos comes in searching Top PHP Interview Questions and Answers for Freshers 2026 Shahrukh Gaffar Shaikh Accolite Digital Welcome Kit 2021 Unboxing: What's Inside? How to Extract RAR Files on Mac: The Ultimate Easy Guide How to Remove 000webhost Watermark Easily: Step-by-Step Realme Buds Air 3 Unboxing: What's Inside the Box? Flipkart not returning product | realtime experience How to claim warranty for realme buds How to Replace boAt Headphones at Service Center: Guide Rajasthan Royals #hallamove trending video Unbelievable Cricket Run Out: The Best You've Never Seen! AI Fiesta – Six Premium AI Models in One Chat How to download Instagram Reels Online Free ? Shrey pro guard air stainless steel cricket helmet unboxing Should we buy Nobero Joggers ? | Unboxing | First Look How to Get 20% OFF on Hostinger + Live Price Comparison Buy Apple product | apple.com | new iPhone 17 | MacBook How to Find a Realme Service Center Near You: Official Site How to Find Apple Service Center in India via Website ZEBRONICS Silencio 111 headphone review Benchmark Education Unboxing of Black Denim review HeadFox N2F Sharp Smart Bluetooth Helmet Unboxing Casence iPhone 15 Crystal Black Case: 3-Month Review Home Sugar Test Kit Unboxing | Dr. Morepen BG-03 Gluco One Glucometer Combo Review Axor Apex Hex-2: Unboxing & First Ride Impressions! EDYELL C8 Pro Motorcycle Bluetooth Headset | Unboxing Atomberg Studio Nexus Unboxing: Smart Fan First Look! 🔥 Budget Shoe Rack from Meesho: Unbox & Organize! Vote Now: Your Most Anticipated Movie of the Year! Rezoni iPhone 15 Anti-Yellow Case: Unboxing & Review AULA F75 Unboxing: The Ultimate 75% Wireless RGB Keyboard

Long Description

In modern web application development, clean, user-friendly, and SEO-optimized URLs play a pivotal role in delivering a great user experience and ranking higher on search engines. By default, the CodeIgniter PHP framework maps incoming browser requests directly using a strict Controller/Method/Parameter URL hierarchy (such as `example.com/product/detail/12`). However, CodeIgniter includes a robust routing engine that allows developers to re-map these default URLs into customized, descriptive paths like `example.com/products/laptops`. Masterful handling of CodeIgniter routing allows you to completely decouple your URL structures from your internal file hierarchy.\n\nTo begin configuring custom routes, you must first locate the designated routing file within your CodeIgniter application directory structure. If you are working with legacy CodeIgniter 3, routes are defined inside the `application/config/routes.php` file using a global array called `$route`. For developers utilizing modern CodeIgniter 4, custom routes are declared in the `app/Config/Routes.php` configuration file using the `$routes` service object. Both versions operate under similar logical principles: matching an incoming URL string pattern and directing it to the correct controller class and method.\n\nThe fundamental syntax for creating a basic, static custom route is straightforward. Suppose you have a controller named `Pages` with a method called `about_us()`, which normally resolves to `example.com/pages/about_us`. To simplify this into a clean URL like `example.com/about`, you define a static route rule. In CodeIgniter 3, you write `$route['about'] = 'pages/about_us';`. In CodeIgniter 4, you use the HTTP verb method `$routes->get('about', 'Pages::about_us');`. Whenever a site visitor accesses `/about`, CodeIgniter routes the request internally to the designated controller method without altering the address bar.\n\nBeyond static pages, real-world web applications heavily depend on dynamic URLs that pass parameters, such as user IDs, product slugs, or blog dates. CodeIgniter provides convenient wildcards and placeholders to match dynamic URI segments effortlessly. The two most common wildcards are `(:num)`, which matches numeric characters only, and `(:any)`, which matches any sequence of characters. For instance, to map a blog post URL like `example.com/blog/my-first-post` to a `Blog` controller's `view($slug)` method, you write `$route['blog/(:any)'] = 'blog/view/$1';` in CodeIgniter 3 or `$routes->get('blog/(:any)', 'Blog::view/$1');` in CodeIgniter 4. The `$1` acts as a back-reference variable containing the value captured by the wildcard placeholder.\n\nFor complex web applications requiring strict validation on parameters, regular expression (regex) routing offers complete control over matching criteria. Instead of standard wildcards, you can insert regex patterns directly into the route key. For example, setting `$route['products/([a-z]+)/([0-9]+)'] = 'products/filter/$1/$2';` ensures that the route executes only if the first parameter consists of lowercase alphabetic characters and the second parameter is strictly numerical. This technique prevents invalid requests from reaching your controllers and adds an extra layer of security.\n\nCodeIgniter 4 introduces advanced routing features that make application architecture even cleaner, such as route grouping and HTTP verb-based routing. Route grouping allows you to wrap administrative or modular routes together under a shared prefix. By defining `$routes->group('admin', function($routes) { ... });`, all nested routes automatically inherit the `/admin/` path prefix. Furthermore, verb-based routes—including `$routes->get()`, `$routes->post()`, `$routes->put()`, and `$routes->delete()`—allow developers to quickly build RESTful API endpoints where identical URIs perform different operations based on the incoming HTTP method.\n\nA common issue developers face when creating custom URLs is the presence of `index.php` in the browser address bar. To achieve truly clean, professional custom URLs, you must remove `index.php` by enabling URL rewriting on your Apache or Nginx web server. On Apache servers, creating a simple `.htaccess` file in your root CodeIgniter directory with `RewriteEngine On` rules routes all incoming web requests through the index file invisibly. Once configured alongside your custom routes file, your application yields short, elegant, and highly performant URLs.\n\nIn conclusion, understanding how to configure custom routing in CodeIgniter is an essential skill for modern PHP developers. Whether you are mapping simple static pages, dynamic blog slugs, or structured administrative portals, custom routes provide the flexibility needed to build scalable, SEO-friendly web applications.

Related Contents