Solution to index. php in the CI framework. htaccess hiding url

Source: Internet
Author: User
CI framework. htaccess hides the index in the url. php solution CodeIgniter (hereinafter referred to as "CI") is a foreign excellent PHP lightweight MVC framework, which supports PHP4 and PHP5, it is a powerful tool for developing small and medium-sized Web applications with high scalability requirements. The blog program you have seen is written using CI.

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

# 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 ErrorDocument404/index. php as if it was not set to hide

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:

1 $ Config ['index _ page'] = "index. php ";

To

1 $ 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:

$ 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.

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

The modified rules are as follows:

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

The rest remains unchanged.


========================================================== ========================================================== ==================================


[Others]The url of the website development framework CodeIgniter

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

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 to 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.hualai.net.cn/index.php/news/view/about into http://www.hualai.net.cn/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:

    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:

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

    Index. php? C = controller & m = method

    This can be used when we use CodeIgniter to create pages.

    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.