Follow me to learn the URL of CodeIgniter, a website development framework

2012/08/09 09:02
Reading number 646

How to delete the index.php file

It is estimated that the first step for many people to learn CodeIgniter is to remove index.php. This official manual has related tutorials to modify the. htaccess file (if your server is Apache):

 RewriteEngine on   RewriteCond $1 !^(index\.php|images|robots\.txt)   RewriteRule ^(.*)$ /index.php/$1 [L]

Of course, many people have modified it as required, but there are errors. All accesses to 404 have been made. Moreover, this 404 is the 404 page of Apache, not the 404 error page of CodeIgniter.

This problem occurs because you do not understand the rewrite rules of Apache:

  • In the first line, set the RewriteEngine engine to on to make the URL rewriting take effect;
  • The second line is to configure url rewriting rules^ (index . Php | images | robots . Txt) This regular expression indicates which files do not need to be rewritten, but are accessed directly;
  • The third line, ^ (. *) $, is a regular expression, which means that all requests are sent to/index.php/$1. Those familiar with urls know that those starting with a backslash (/) are relative paths. To whom? The root is the web address.

Therefore, if CodeIgniter is not installed in the root directory of the website, errors will inevitably occur. How to solve this problem? The CodeIgniter manual also provides the corresponding solutions:

Change the last sentence above to read:

 RewriteRule ^(.*)$ index.php/$1 [L]

Just remove the slash in front of index. php.

How to add url suffix

Through the above steps, we have hidden index.php. Now our website is more restful. Most people can't tell whether your website is developed with CodeIgniter or ROR at a glance.

However, how to add a suffix after the URL? In this way, we can even hide or forge the website development language. By modifying the config/config.php file, you can add a specified file suffix to the URL generated by CodeIgniter, such as. html, even. asp,. jsp.

So that we can http://www.hualai.net.cn/index.php/news/view/about   Become http://www.hualai.net.cn/index.php/news/view/about.html Has. How to use query strings

Generally, we don't need to use query strings, but there are always some special cases that we can't complete with the rest mode of CodeIgniter, so we need to use query strings in URLs:

 index.php?c=products&m=view&id=345

  CodeIgniter is disabled by default. If you want to enable it, open the configuration file application/config/config.php, and you can see the following:

 $config['enable_query_strings'] = FALSE;   $config['controller_trigger'] = 'c'; // Controller name $config['function_trigger'] = 'm'; // Method name $config['directory_trigger']='d'; // Name of the subdirectory where the controller is located

  If you change enable_query_strings to TRUE, this function will be activated. At this point, you can call the required controller and method through the keyword:

 index.php?c=controller&m=method

  This is useful when we use CodeIgniter to make pagination.

Expand to read the full text
Loading
Click to lead the topic 📣 Post and join the discussion 🔥
Reward
zero comment
three Collection
three fabulous
 Back to top
Top