Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ################ |
| 2 | CodeIgniter URLs |
| 3 | ################ |
| 4 | |
| 5 | By default, URLs in CodeIgniter are designed to be search-engine and |
| 6 | human friendly. Rather than using the standard "query string" approach |
| 7 | to 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 | |
| 15 | URI Segments |
| 16 | ============ |
| 17 | |
| 18 | The segments in the URL, in following with the Model-View-Controller |
| 19 | approach, usually represent:: |
| 20 | |
| 21 | example.com/class/function/ID |
| 22 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 23 | #. 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 Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 30 | The :doc:`URI Library <../libraries/uri>` and the :doc:`URL Helper |
| 31 | <../helpers/url_helper>` contain functions that make it easy to work |
| 32 | with your URI data. In addition, your URLs can be remapped using the |
| 33 | :doc:`URI Routing <routing>` feature for more flexibility. |
Andrey Andreev | d1097a1 | 2012-11-01 19:55:42 +0200 | [diff] [blame] | 34 | |
| 35 | Friendly URLs |
| 36 | ============= |
| 37 | |
| 38 | As you might guess, since there's a straight relationship between |
| 39 | URI segments and the controller/method pair that's being called, |
| 40 | those two determining segments must represent a valid class and |
| 41 | method name. |
| 42 | You may however also use dashes in the class/method-representing |
| 43 | segments, and they will automatically be translated to underscores |
| 44 | in order to be valid routed segments. |
| 45 | |
| 46 | For example:: |
| 47 | |
| 48 | example.com/my-settings/change-password/ |
| 49 | |
| 50 | The above example will route to the ``My_settings`` controller and |
| 51 | its method ``change_password()``. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 52 | |
| 53 | Removing the index.php file |
| 54 | =========================== |
| 55 | |
| 56 | By default, the **index.php** file will be included in your URLs:: |
| 57 | |
| 58 | example.com/index.php/news/article/my_article |
| 59 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 60 | If your Apache server has *mod_rewrite* enabled, you can easily remove this |
CroNiX | 624010f | 2012-01-25 14:52:11 -0800 | [diff] [blame] | 61 | file by using a .htaccess file with some simple rules. Here is an example |
| 62 | of such a file, using the "negative" method in which everything is redirected |
| 63 | except the specified items: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 64 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 65 | :: |
| 66 | |
Repox | ed47727 | 2011-11-22 11:13:48 +0100 | [diff] [blame] | 67 | RewriteEngine On |
| 68 | RewriteCond %{REQUEST_FILENAME} !-f |
| 69 | RewriteCond %{REQUEST_FILENAME} !-d |
insign | 5287f66 | 2012-01-09 18:07:34 -0200 | [diff] [blame] | 70 | RewriteRule ^(.*)$ index.php/$1 [L] |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 71 | |
Repox | 1f9a40f | 2011-11-22 11:25:24 +0100 | [diff] [blame] | 72 | In the above example, any HTTP request other than those for existing |
Repox | ed47727 | 2011-11-22 11:13:48 +0100 | [diff] [blame] | 73 | directories and existing files is treated as a request for your index.php file. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 74 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 75 | .. note:: These specific rules might not work for all server configurations. |
| 76 | |
| 77 | .. note:: Make sure to also exclude from the above rule any assets that you |
| 78 | might need to be accessible from the outside world. |
CroNiX | 624010f | 2012-01-25 14:52:11 -0800 | [diff] [blame] | 79 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 80 | Adding a URL Suffix |
| 81 | =================== |
| 82 | |
purwandi | 69116ed | 2011-10-07 15:27:45 +0700 | [diff] [blame] | 83 | In your **config/config.php** file you can specify a suffix that will be |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 84 | added to all URLs generated by CodeIgniter. For example, if a URL is |
| 85 | this:: |
| 86 | |
| 87 | example.com/index.php/products/view/shoes |
| 88 | |
purwandi | 69116ed | 2011-10-07 15:27:45 +0700 | [diff] [blame] | 89 | You can optionally add a suffix, like **.html,** making the page appear to |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 90 | be of a certain type:: |
| 91 | |
| 92 | example.com/index.php/products/view/shoes.html |
| 93 | |
| 94 | Enabling Query Strings |
| 95 | ====================== |
| 96 | |
| 97 | In some cases you might prefer to use query strings URLs:: |
| 98 | |
| 99 | index.php?c=products&m=view&id=345 |
| 100 | |
| 101 | CodeIgniter optionally supports this capability, which can be enabled in |
purwandi | 69116ed | 2011-10-07 15:27:45 +0700 | [diff] [blame] | 102 | your **application/config.php** file. If you open your config file you'll |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 103 | see these items:: |
| 104 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 105 | $config['enable_query_strings'] = FALSE; |
| 106 | $config['controller_trigger'] = 'c'; |
| 107 | $config['function_trigger'] = 'm'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 108 | |
| 109 | If you change "enable_query_strings" to TRUE this feature will become |
| 110 | active. Your controllers and functions will then be accessible using the |
| 111 | "trigger" words you've set to invoke your controllers and methods:: |
| 112 | |
| 113 | index.php?c=controller&m=method |
| 114 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 115 | .. note:: If you are using query strings you will have to build your own |
| 116 | URLs, rather than utilizing the URL helpers (and other helpers |
| 117 | that generate URLs, like some of the form helpers) as these are |
| 118 | designed to work with segment based URLs. |