blob: b8a1b9f5bfd88313e645b2d6d84470c1feb6f039 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001################
2CodeIgniter URLs
3################
4
5By default, URLs in CodeIgniter are designed to be search-engine and
6human friendly. Rather than using the standard "query string" approach
7to URLs that is synonymous with dynamic systems, CodeIgniter uses a
8**segment-based** approach::
9
10 example.com/news/article/my_article
11
12.. note:: Query string URLs can be optionally enabled, as described
13 below.
14
15URI Segments
16============
17
18The segments in the URL, in following with the Model-View-Controller
19approach, usually represent::
20
21 example.com/class/function/ID
22
Derek Jones8ede1a22011-10-05 13:34:52 -050023#. The first segment represents the controller **class** that should be
24 invoked.
25#. The second segment represents the class **function**, or method, that
26 should be called.
27#. The third, and any additional segments, represent the ID and any
28 variables that will be passed to the controller.
29
Andrey Andreev16a704c2012-11-09 17:25:00 +020030The :doc:`URI Library <../libraries/uri>` and the :doc:`URL Helper
31<../helpers/url_helper>` contain functions that make it easy to work
32with your URI data. In addition, your URLs can be remapped using the
33:doc:`URI Routing <routing>` feature for more flexibility.
Andrey Andreevd1097a12012-11-01 19:55:42 +020034
Derek Jones8ede1a22011-10-05 13:34:52 -050035Removing the index.php file
36===========================
37
38By default, the **index.php** file will be included in your URLs::
39
40 example.com/index.php/news/article/my_article
41
Andrey Andreev16a704c2012-11-09 17:25:00 +020042If your Apache server has *mod_rewrite* enabled, you can easily remove this
CroNiX624010f2012-01-25 14:52:11 -080043file by using a .htaccess file with some simple rules. Here is an example
44of such a file, using the "negative" method in which everything is redirected
Andrey Andreev2e0247a2017-10-09 11:00:03 +030045except the specified items:
Derek Jones9607e732011-10-05 16:42:42 -050046
Andrey Andreev2e0247a2017-10-09 11:00:03 +030047.. code-block:: apache
Andrey Andreev954c4aa2017-07-21 11:51:37 +030048
Repoxed477272011-11-22 11:13:48 +010049 RewriteEngine On
50 RewriteCond %{REQUEST_FILENAME} !-f
51 RewriteCond %{REQUEST_FILENAME} !-d
insign5287f662012-01-09 18:07:34 -020052 RewriteRule ^(.*)$ index.php/$1 [L]
Derek Jones8ede1a22011-10-05 13:34:52 -050053
Repox1f9a40f2011-11-22 11:25:24 +010054In the above example, any HTTP request other than those for existing
Repoxed477272011-11-22 11:13:48 +010055directories and existing files is treated as a request for your index.php file.
Derek Jones8ede1a22011-10-05 13:34:52 -050056
Andrey Andreev16a704c2012-11-09 17:25:00 +020057.. note:: These specific rules might not work for all server configurations.
58
59.. note:: Make sure to also exclude from the above rule any assets that you
60 might need to be accessible from the outside world.
CroNiX624010f2012-01-25 14:52:11 -080061
Derek Jones8ede1a22011-10-05 13:34:52 -050062Adding a URL Suffix
63===================
64
purwandi69116ed2011-10-07 15:27:45 +070065In your **config/config.php** file you can specify a suffix that will be
Derek Jones8ede1a22011-10-05 13:34:52 -050066added to all URLs generated by CodeIgniter. For example, if a URL is
67this::
68
69 example.com/index.php/products/view/shoes
70
purwandi69116ed2011-10-07 15:27:45 +070071You can optionally add a suffix, like **.html,** making the page appear to
Derek Jones8ede1a22011-10-05 13:34:52 -050072be of a certain type::
73
74 example.com/index.php/products/view/shoes.html
75
76Enabling Query Strings
77======================
78
79In some cases you might prefer to use query strings URLs::
80
81 index.php?c=products&m=view&id=345
82
83CodeIgniter optionally supports this capability, which can be enabled in
purwandi69116ed2011-10-07 15:27:45 +070084your **application/config.php** file. If you open your config file you'll
Derek Jones8ede1a22011-10-05 13:34:52 -050085see these items::
86
Derek Jones9607e732011-10-05 16:42:42 -050087 $config['enable_query_strings'] = FALSE;
88 $config['controller_trigger'] = 'c';
89 $config['function_trigger'] = 'm';
Derek Jones8ede1a22011-10-05 13:34:52 -050090
91If you change "enable_query_strings" to TRUE this feature will become
92active. Your controllers and functions will then be accessible using the
93"trigger" words you've set to invoke your controllers and methods::
94
95 index.php?c=controller&m=method
96
Andrey Andreev16a704c2012-11-09 17:25:00 +020097.. note:: If you are using query strings you will have to build your own
98 URLs, rather than utilizing the URL helpers (and other helpers
99 that generate URLs, like some of the form helpers) as these are
100 designed to work with segment based URLs.