What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
In CodeIgniter 3, retrieve a URI segment with $this->uri->segment($n), numbering from 1 at the left of the application URI. But if the practical problem is a login link returning 404 after a site moves into a subdirectory, inspect the link’s generated href and URL configuration before changing segment parsing.
How CodeIgniter 3 numbers URI segments
CodeIgniter 3.1.13 documents a segment-based URL convention: the first segment usually identifies a controller, the second a method, and later segments commonly carry values such as IDs. Routes can remap that relationship, so segment position does not by itself prove which controller or method will run. See the CodeIgniter 3 URL guide.
As an Amazon Associate I earn from qualifying purchases.
The URI class numbers segments from left to right, starting at 1. For example, $this->uri->segment(1) requests the first segment and $this->uri->segment(2) requests the second. Its segment($n, $no_result) method also accepts a fallback for a missing segment. The guide documents access to re-routed segments as well. See the CodeIgniter 3 URI class documentation.
Why a subdirectory move can expose a broken login link
A SitePoint thread from June 25, 2012 describes a CodeIgniter site moved from the domain root beneath /demo/Baxobeat. The author initially asked how to adjust uses of $this->uri->segment(2) for a page name and $this->uri->segment(1) for a controller name. A reply suggested splitting $_SERVER['REQUEST_URI'] with explode(), but the reported symptom then became a 404 from the login link. A later response pointed to the HTML link; the original poster found the anchor in the header and said it was formed using base_url() and home/logIn. The thread does not provide enough configuration or server details to establish the precise cause of that 404. See the SitePoint discussion.
#1 Best Overall
That sequence matters: a raw request-path parsing suggestion was not shown to fix the failing link. First determine whether the request reaches the expected route at all. Inspect the actual rendered link in the browser, rather than assuming the segment-reading code is responsible.
Choose between site_url() and base_url()
CodeIgniter 3’s URL Helper distinguishes the helpers by what they build. site_url() is intended for local application URLs and can include configured index_page and url_suffix values. base_url() returns the base URL without those additions. Both accept URI segments. Use the helper that fits the intended URL and the installation’s settings; the historical thread does not establish a universal replacement line. See the CodeIgniter 3 URL Helper documentation.
Rank #2
| Helper | Documented behavior | Useful when checking |
|---|---|---|
site_url() |
Builds a local application URL and may include configured index_page and url_suffix. |
The link should address an application route, including configured URL components. |
base_url() |
Returns the base URL without configured index_page and url_suffix; accepts URI segments. |
The value is intended to be base-relative, and you need to verify how the resulting path maps to the application. |
Diagnose the 404 in a useful order
- Inspect the rendered anchor. Find the login link in the header and copy its actual
href. Check whether it points into the deployed subdirectory and whether it includes any configured index page or suffix needed by the application. - Check the base URL and index-page settings. Compare the generated link with the deployed application location and the CodeIgniter configuration. A helper can only produce the expected path when its inputs and configuration match the installation.
- Check routes and method names. Confirm that the destination corresponds to a valid controller/method or an explicit route mapping. CodeIgniter’s conventional first-segment/second-segment interpretation can be changed by routing.
- Check rewrite behavior. Review the web-server rewrite setup and whether the deployed URL form is expected to include an index page. The forum thread does not include the rules needed to prescribe a specific rewrite configuration.
- Only then revisit segment-reading code. If the destination is correct but application logic reads the wrong part of the URI, verify the processed URI and the relevant numbered segment using the CodeIgniter URI class.
Why explode() on REQUEST_URI is not a universal fix
explode('/', $_SERVER['REQUEST_URI']) splits a raw request-URI string; it does not apply CodeIgniter’s route mapping or establish that an HTML link points to a valid application route. The SitePoint reply offered this as a suggestion, but the thread does not show it resolving the login 404. For framework-level segments, use the URI class and account for routing. For a broken link, start with the generated href, helper choice, base URL, index-page setting, and rewrite setup.
Recommended Free Tools
Quick Recap
Rank #4
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




