CodeIgniter (CI) is a lightweight php mvc framework that supports PHP4 and PHP5. it is a powerful tool for developing small and medium-sized Web applications with high scalability requirements. Many blog programs and open-source cms programs are written using CI. CI framework
Adhering to the idea of MVC architecture, all controllers in CI need to load the call through the single point of entry file index. php (default. That is to say, by default, the URLs of all CI development projects are in the following format:
Http: // localhost/index. php/blog/logs/this_is_a_test_entry
Obviously, the existence of index. php in the URL segment affects the simplicity of the URL and SEO by default. We can remove this annoying Index. php through the method described below.
You may have noticed that there is a solution to this problem in the CodeIgniter User Manual. However, this. htaccess configuration officially provided does not solve the problem all the time. This article provides a better solution.
Note: Before proceeding, confirm that your host supports. htaccess configuration. If Apache is used as the Web server, the mod_rewrite module must be enabled. if IIS is used as the Web server, the ISAPI_Rewrite extension must be installed.
The specific method is as follows:
1. copy and save the following configuration information as a. htaccess file.
The. htaccess file information is as follows:
The code is as follows:
RewriteEngineOn
RewriteBase/
RewriteCond % {REQUEST_FILENAME }! -F
RewriteCond % {REQUEST_FILENAME }! -D
RewriteRule ^ (. *) $/index. php? /$1 [L]
# If the mod_rewrite module is not installed, all the 404 pages will be # sent to index. php. at this time, the program will run as if it was not set to hide
ErrorDocument404/index. php
2. Upload the above. htaccess file to the root directory of the project where CI is located (that is, under the same directory as index. php)
3. modify the following parameters in application/config. php:
The code is as follows:
$ Config ['index _ page'] = "index. php ";
To
The code is as follows:
$ Config ['index _ page'] = ""; // The value is null.
The preceding three steps are indispensable. If everything is configured properly, you will find that when you run the program again, the program has automatically hidden the index. php URL segment!
Trackback (UTF-8): http://www.cnSaturn.com/trackback/40
Mod_rewrite hides index. php when PATH_INFO is enabled in CodeIgniter.
In CodeIgniter, when I change the URI addressing mode from AUTO to PATH_INFO, that is:
The code is as follows:
$ Config ['uri _ protocol'] = 'path _ info ';
Note: PATH_INFO is enabled because I want to use $ _ GET instead of the default POST method.
In this case, how can I still use the above. htaccess scheme? the result will be: index. php is hidden smoothly, but the master controller cannot get the correct value.
The solution is as follows:
Remove the question mark after index. php in the following rewrite rules.
The code is as follows:
RewriteRule ^ (. *) $/index. php? /$1 [L]
The modified rules are as follows:
The code is as follows:
RewriteRule ^ (. *) $/index. php/$1 [L]
The rest remains unchanged.
How to delete the index. php file
It is estimated that many people will learn how to remove index. php in the first step of CodeIgniter. this official manual provides relevant tutorials to modify the. htaccess file (the premise is that your server is apache ):
The code is as follows:
RewriteEngine on
RewriteCond $1! ^ (Index \. php | images | robots \. txt)
RewriteRule ^ (. *) $/index. php/$1 [L]
Of course, a lot of people have modified the code as required, but there are errors. all accesses are 404. Moreover, this 404 is the apache 404 page, rather than the CodeIgniter 404 error page.
This problem occurs because you do not understand apache rewrite rules:
The first line is to set the RewriteEngine engine to on to make url rewriting take effect;
Second, configure url rewriting rules ,! ^ (Index \. php | images | robots \. txt) this regular expression specifies which files do not need to be overwritten, but are accessed directly;
Row 3, ^ (. *) $ is a regular expression, meaning that all requests are sent to/index. php/$1. anyone familiar with URLs knows that all the URLs starting with a backslash (/) are relative paths? Root, that is, the URL.
Therefore, if CodeIgniter is not installed in the root directory of the website, an error will inevitably occur. How can we solve this problem? the corresponding solutions are also provided in the CodeIgniter manual:
Change the last sentence:
The code is as follows:
RewriteRule ^ (. *) $ index. php/$1 [L]
You only need to remove the slash before index. php.
How to add a url suffix
Through the above steps, we have hidden the index. php, now our website is even more restful. most people can't see at a glance whether your website was developed with CodeIgniter or ROR.
However, how can we add a suffix after a url? in this way, we can even hide or forge the website development language by modifying config/config. php file. you can specify a suffix for the URL generated by CodeIgniter. asp ,. jsp.
So we can turn the http://www.bitsCN.com/index.php/news/view/about into http://www.bitscn.com/index.php/news/view/about.html.
How to use a query string
In general, we do not need to use query strings. However, there are always some special situations where we cannot do this using CodeIgniter's rest mode. in this way, we need to use the query string in the URL:
The code is as follows:
Index. php? C = products & m = view & id = 345.
CodeIgniter is disabled by default. to enable this function, open the configuration file application/config. php. you can see the following content:
The code is as follows:
$ 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 is activated. In this case, you can use keywords to call the desired controller and method:
The code is as follows:
Index. php? C = controller & m = method
This can be used when we use CodeIgniter to create pages.