URL Routing
URLs in CodeIgniter are designed to be short and search engine friendly. It should make more sense to the visitors. A user should get an idea about the page content through its URL.
For example, http://abc.com/codeigniter/routing_url
The above URL example makes more sense and gives a brief idea to the users what it is about.
One should always go for the SEO friendly URL.
URL routing is a technique through which it converts SEO friendly URLs into a server code format that understands it easily and drives a request to corresponding handler scripts.
Setting your own Routing Rules
Routing rules are defined in routes.php file at the location application/config. In this file you’ll see $route array, it permits you to specify your own routing criteria. Routes can be classified in two ways, either using Wildcards or Regular Expressions.
Wildcards
There are two types of wildcards:
- :num−series containing only numbers will be matched.
- :any−series containing only characters will be matched.
Using :num
URL containing first segment as ‘blog’ and second segment as any ‘number’ will represent to the URL containing ‘women’ class and ‘social’ method passing in the match as the variable to the function.
It means when we’ll pass URL http://www.abc.com/blog/1
Note: Here, you can pass any number instead of 1 in the URL.
It will be directed to http://www.abc.com/women/social
Using :any
URL containing first segment as ‘blog’ and second segment as anything will represent to the URL containing ‘women’ class and ‘social’ method.
It means when we’ll pass URL http://www.abc.com/blog/xyz
Note: Here, you can pass anything in the last segment of URL.
It will be directed to http://www.abc.com/women/social
Regular Expression
Regular expressions are also used to redirect routes.
You can create your own regular expression to run your URL.
URL Suffix
To add a suffix in your URL, go to file config.php in application/config folder and add suffix you want as shown below. We have added .jsp as the suffix.
For example, if our URL ishttp://www.abc.com/women/social
Then after adding suffix, our URL will becomehttp://www.abc.com/women/social.jsp