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