blob: 20f80632a7815ebc8da2bab789bc18ce978b5b56 [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
23
24#. The first segment represents the controller **class** that should be
25 invoked.
26#. The second segment represents the class **function**, or method, that
27 should be called.
28#. The third, and any additional segments, represent the ID and any
29 variables that will be passed to the controller.
30
Andrey Andreevd1097a12012-11-01 19:55:42 +020031The :doc:`URI Class <../libraries/uri>` and the :doc:`URL Helper <../helpers/url_helper>`
32contain functions that make it easy to work with your URI data. In addition,
33your URLs can be remapped using the :doc:`URI Routing <routing>` feature for
34more flexibility.
35
36Friendly URLs
37=============
38
39As you might guess, since there's a straight relationship between
40URI segments and the controller/method pair that's being called,
41those two determining segments must represent a valid class and
42method name.
43You may however also use dashes in the class/method-representing
44segments, and they will automatically be translated to underscores
45in order to be valid routed segments.
46
47For example::
48
49 example.com/my-settings/change-password/
50
51The above example will route to the ``My_settings`` controller and
52its method ``change_password()``.
Derek Jones8ede1a22011-10-05 13:34:52 -050053
54Removing the index.php file
55===========================
56
57By default, the **index.php** file will be included in your URLs::
58
59 example.com/index.php/news/article/my_article
60
CroNiX624010f2012-01-25 14:52:11 -080061If your Apache server has mod_rewrite enabled, you can easily remove this
62file by using a .htaccess file with some simple rules. Here is an example
63of such a file, using the "negative" method in which everything is redirected
64except the specified items:
Derek Jones8ede1a22011-10-05 13:34:52 -050065
Derek Jones9607e732011-10-05 16:42:42 -050066::
67
Repoxed477272011-11-22 11:13:48 +010068 RewriteEngine On
69 RewriteCond %{REQUEST_FILENAME} !-f
70 RewriteCond %{REQUEST_FILENAME} !-d
insign5287f662012-01-09 18:07:34 -020071 RewriteRule ^(.*)$ index.php/$1 [L]
Derek Jones8ede1a22011-10-05 13:34:52 -050072
Repox1f9a40f2011-11-22 11:25:24 +010073In the above example, any HTTP request other than those for existing
Repoxed477272011-11-22 11:13:48 +010074directories and existing files is treated as a request for your index.php file.
Derek Jones8ede1a22011-10-05 13:34:52 -050075
CroNiX624010f2012-01-25 14:52:11 -080076.. note:: Note: These specific rules might not work for all server configurations.
77
Derek Jones8ede1a22011-10-05 13:34:52 -050078Adding a URL Suffix
79===================
80
purwandi69116ed2011-10-07 15:27:45 +070081In your **config/config.php** file you can specify a suffix that will be
Derek Jones8ede1a22011-10-05 13:34:52 -050082added to all URLs generated by CodeIgniter. For example, if a URL is
83this::
84
85 example.com/index.php/products/view/shoes
86
purwandi69116ed2011-10-07 15:27:45 +070087You can optionally add a suffix, like **.html,** making the page appear to
Derek Jones8ede1a22011-10-05 13:34:52 -050088be of a certain type::
89
90 example.com/index.php/products/view/shoes.html
91
92Enabling Query Strings
93======================
94
95In some cases you might prefer to use query strings URLs::
96
97 index.php?c=products&m=view&id=345
98
99CodeIgniter optionally supports this capability, which can be enabled in
purwandi69116ed2011-10-07 15:27:45 +0700100your **application/config.php** file. If you open your config file you'll
Derek Jones8ede1a22011-10-05 13:34:52 -0500101see these items::
102
Derek Jones9607e732011-10-05 16:42:42 -0500103 $config['enable_query_strings'] = FALSE;
104 $config['controller_trigger'] = 'c';
105 $config['function_trigger'] = 'm';
Derek Jones8ede1a22011-10-05 13:34:52 -0500106
107If you change "enable_query_strings" to TRUE this feature will become
108active. Your controllers and functions will then be accessible using the
109"trigger" words you've set to invoke your controllers and methods::
110
111 index.php?c=controller&m=method
112
purwandi69116ed2011-10-07 15:27:45 +0700113.. note:: If you are using query strings you will have to build
Derek Jones9607e732011-10-05 16:42:42 -0500114 your own URLs, rather than utilizing the URL helpers (and other helpers
115 that generate URLs, like some of the form helpers) as these are designed
Andrey Andreevd1097a12012-11-01 19:55:42 +0200116 to work with segment based URLs.