CodeIgniter (hereinafter referred to as "CI") is an excellent foreign PHP lightweight MVC framework that supports PHP4 and PHP5, and is a powerful tool for developing small and medium-sized Web applications with high scalability requirements. At present, you see this blog program is the use of CI to write.
Adhering to the idea of the MVC architecture, all controllers in CI need to be loaded with a single point of entry file index.php (default) to load the call. That is, by default, all CI development projects have URLs that are in the form of the following:
Http://localhost/index.php/blog/logs/this_is_a_test_entry
Obviously, by default, the presence of index.php in the URL address segment affects the simplicity of the URL and the SEO. We can get rid of this nasty index.php by the method described in the article below.
You may have noticed that there is already a workaround for this issue in the CodeIgniter user manual. But the official offer of this. htaccess configuration, not all the time to solve the problem. This article now gives a more complete solution.
Note: before continuing, confirm that your host supports the. htaccess configuration. If Apache is a Web server, you need to turn on support for the Mod_rewrite module, and if you use IIS as a Web server, you will need to install additional isapi_rewrite extensions.
Here's how:
1. Copy and save the following configuration information as a. htaccess file.
The following is the. htaccess file information |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Rewriteengineon rewritebase / Rewritecond %{request_filename} ! -F rewritecond % { request_ Filename ! -D   rewriterule^(. *)$ /index.php? /$ 1 [L ] #如果没有安装mod_rewrite模块, all 404 pages will be #发送到index. php, at this point, the program will run as if it were 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, with the index.php sibling directory)
3. Modify the following parameters in the application/config.php:
1 |
$config [ Index_page ' = " index.php " |
To
1 |
$config [' index_page '] = ""; //Set to empty |
The above three steps, indispensable. If everything is properly configured, you will find that when you run the program again, the program has automatically hidden index.php this URL segment!
Trackback (UTF-8): HTTP://WWW.CNSATURN.COM/TRACKBACK/40
mod_rewrite to hide index.php when Path_info is turned on in CodeIgniter。
In CodeIgniter, when I change the URI addressing mode from auto to Path_info, that is:
$config [' Uri_protocol '] = ' path_info ';
Note: The path_info is turned on because I want to use $_get to get the value, not the default post mode of the system.
In this case how to still use the above. htaccess scenario, the result will be: The index.php is successfully hidden, but the host controller does not get the value correctly.
Here's the solution, one step:
Remove the question mark after index.php in the following rewrite rule.
rewriterule^ (. *) $/index.php? /$1[L]
The revised rules are as follows:
rewriterule^ (. *) $/index.php/$1 [L]
Other places do not change.
=============================================================================================================
"Other" Follow me to learn the website Development Framework CodeIgniter URL Chapter
How to delete index.php files
It is estimated that many people learn codeigniter the first thing to do is how to get rid of index.php, this official manual has related tutorials, modify the. htaccess file (provided your server is Apache):
Rewriteengine on
Rewritecond $!^ (index\.php|images|robots\.txt)
Rewriterule ^ (. *) $/index.php/$1 [L]
Of course, a lot of people changed as required, but there was an error, all the visits were 404, and the 404 was Apache's 404 page, not the CodeIgniter 404 error page.
This problem is not understood by the Apache rewrite rule:
- The first line, setting the Rewriteengine engine to On, is for URL rewriting to take effect;
- The second line, configure URL rewrite rules,!^ (index\.php|images|robots\.txt) This regular expression indicates which files do not need to be rewritten, but direct access;
- The third line, ^ (. *) $ is a regular expression, meaning that all requests are sent to/index.php/$1, and anyone familiar with the URL knows that a backslash (/) begins with a relative path, relative to whom? Root, which is the URL.
So, if CodeIgniter is not installed in the root directory of the Web site, there will inevitably be an error. How to solve this, the corresponding solution is also given in the CodeIgniter manual:
Change the last sentence above to read: Rewriterule ^ (. *) $ index.php/$1 [L]
Just remove the slash in front of the index.php.
How to add a URL suffix
Through the above steps, we have hidden the index.php, now we make the site more rest, the average person can not see at a glance your site is developed with CodeIgniter, or ROR developed.
However, how to add a suffix after the URL, so that we can even hide or forge the Web site's development language, by modifying the config/config.php file, you can add a specified file suffix for the codeigniter generated URL, For example, you can add. html, even you can add. asp,.jsp.
So we can turn http://www.hualai.net.cn/index.php/news/view/about into http://www.hualai.net.cn/index.php/news/ View/about.html.
How to use query strings
In general, we don't need to use query strings, but there are some special cases that we can't do with CodeIgniter rest mode, so we need to use a query string in the URL:
index.php?c=products&m=view&id=345
CodeIgniter This feature is turned off by default, if you want to turn it on, open the configuration file application/config/config.php 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 resides
If you change enable_query_strings to TRUE, then this function is activated. At this point, you can invoke the required controllers and methods with the keyword:
Index.php?c=controller&m=method
This can come in handy when we're using codeigniter to make pagination.