Teaches you how to use the. htaccess hidden URL in the CI framework index.php_php

Source: Internet
Author: User
Keywords CI Framework
Tags codeigniter
CI Framework

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

Copy the Code code as follows:
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:

Copy the Code code as follows:
$config [' index_page '] = "index.php";

To

Copy the Code code as follows:
$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 hides 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:

Copy the Code code as follows:
$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.

Copy the Code code as follows:
rewriterule^ (. *) $/index.php?/$1[l]

The revised rules are as follows:

Copy the Code code as follows:
rewriterule^ (. *) $/index.php/$1 [L]

Other places do not change.

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):

Copy the Code code as follows:
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:

Replace the last sentence above with the following:
Copy the Code code as follows:
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.bitsCN.com/index.php/news/view/about into http://www.bitsCN.com/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:

Copy the Code code as follows:
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:

Copy the Code code 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 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:

Copy the Code code as follows:
Index.php?c=controller&m=method

This can come in handy when we're using codeigniter to make pagination.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.