blob: 6b390b559e6c6f6ca002bd18d3fe4d3e78d81a35 [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
purwandi69116ed2011-10-07 15:27:45 +070031The :doc:`URI Class <../libraries/uri>` and the :doc:`URL Helper <../helpers/url_helper>` contain functions that make it
Derek Jones8ede1a22011-10-05 13:34:52 -050032easy to work with your URI data. In addition, your URLs can be remapped
33using the :doc:`URI Routing <routing>` feature for more flexibility.
34
35Removing 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
CroNiX624010f2012-01-25 14:52:11 -080042If your Apache server has mod_rewrite enabled, you can easily remove this
43file 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
45except the specified items:
Derek Jones8ede1a22011-10-05 13:34:52 -050046
Derek Jones9607e732011-10-05 16:42:42 -050047::
48
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
CroNiX624010f2012-01-25 14:52:11 -080057.. note:: Note: These specific rules might not work for all server configurations.
58
Derek Jones8ede1a22011-10-05 13:34:52 -050059Adding a URL Suffix
60===================
61
purwandi69116ed2011-10-07 15:27:45 +070062In your **config/config.php** file you can specify a suffix that will be
Derek Jones8ede1a22011-10-05 13:34:52 -050063added to all URLs generated by CodeIgniter. For example, if a URL is
64this::
65
66 example.com/index.php/products/view/shoes
67
purwandi69116ed2011-10-07 15:27:45 +070068You can optionally add a suffix, like **.html,** making the page appear to
Derek Jones8ede1a22011-10-05 13:34:52 -050069be of a certain type::
70
71 example.com/index.php/products/view/shoes.html
72
73Enabling Query Strings
74======================
75
76In some cases you might prefer to use query strings URLs::
77
78 index.php?c=products&m=view&id=345
79
80CodeIgniter optionally supports this capability, which can be enabled in
purwandi69116ed2011-10-07 15:27:45 +070081your **application/config.php** file. If you open your config file you'll
Derek Jones8ede1a22011-10-05 13:34:52 -050082see these items::
83
Derek Jones9607e732011-10-05 16:42:42 -050084 $config['enable_query_strings'] = FALSE;
85 $config['controller_trigger'] = 'c';
86 $config['function_trigger'] = 'm';
Derek Jones8ede1a22011-10-05 13:34:52 -050087
88If you change "enable_query_strings" to TRUE this feature will become
89active. Your controllers and functions will then be accessible using the
90"trigger" words you've set to invoke your controllers and methods::
91
92 index.php?c=controller&m=method
93
purwandi69116ed2011-10-07 15:27:45 +070094.. note:: If you are using query strings you will have to build
Derek Jones9607e732011-10-05 16:42:42 -050095 your own URLs, rather than utilizing the URL helpers (and other helpers
96 that generate URLs, like some of the form helpers) as these are designed
97 to work with segment based URLs.