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 | |
| 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 | |
| 31 | The :doc::doc:`URI Class <../libraries/uri>` and the `URL |
| 32 | Helper <../helpers/url_helper>` contain functions that make it |
| 33 | easy to work with your URI data. In addition, your URLs can be remapped |
| 34 | using the :doc:`URI Routing <routing>` feature for more flexibility. |
| 35 | |
| 36 | Removing the index.php file |
| 37 | =========================== |
| 38 | |
| 39 | By default, the **index.php** file will be included in your URLs:: |
| 40 | |
| 41 | example.com/index.php/news/article/my_article |
| 42 | |
| 43 | You can easily remove this file by using a .htaccess file with some |
| 44 | simple rules. Here is an example of such a file, using the "negative" |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 45 | method in which everything is redirected except the specified items: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 46 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 47 | :: |
| 48 | |
| 49 | RewriteEngine on |
| 50 | RewriteCond $1 !^(index\.php|images|robots\.txt) |
| 51 | RewriteRule ^(.*)$ /index.php/$1 [L] |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 52 | |
| 53 | In the above example, any HTTP request other than those for index.php, |
| 54 | images, and robots.txt is treated as a request for your index.php file. |
| 55 | |
| 56 | Adding a URL Suffix |
| 57 | =================== |
| 58 | |
| 59 | In your config/config.php file you can specify a suffix that will be |
| 60 | added to all URLs generated by CodeIgniter. For example, if a URL is |
| 61 | this:: |
| 62 | |
| 63 | example.com/index.php/products/view/shoes |
| 64 | |
| 65 | You can optionally add a suffix, like .html, making the page appear to |
| 66 | be of a certain type:: |
| 67 | |
| 68 | example.com/index.php/products/view/shoes.html |
| 69 | |
| 70 | Enabling Query Strings |
| 71 | ====================== |
| 72 | |
| 73 | In some cases you might prefer to use query strings URLs:: |
| 74 | |
| 75 | index.php?c=products&m=view&id=345 |
| 76 | |
| 77 | CodeIgniter optionally supports this capability, which can be enabled in |
| 78 | your application/config.php file. If you open your config file you'll |
| 79 | see these items:: |
| 80 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 81 | $config['enable_query_strings'] = FALSE; |
| 82 | $config['controller_trigger'] = 'c'; |
| 83 | $config['function_trigger'] = 'm'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 84 | |
| 85 | If you change "enable_query_strings" to TRUE this feature will become |
| 86 | active. Your controllers and functions will then be accessible using the |
| 87 | "trigger" words you've set to invoke your controllers and methods:: |
| 88 | |
| 89 | index.php?c=controller&m=method |
| 90 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 91 | ..note:: If you are using query strings you will have to build |
| 92 | your own URLs, rather than utilizing the URL helpers (and other helpers |
| 93 | that generate URLs, like some of the form helpers) as these are designed |
| 94 | to work with segment based URLs. |