Teach you how to use in CI frames. htaccess hidden URLs index.php_php Tips

Source: Internet
Author: User
Tags codeigniter

With the idea of the MVC architecture, all controllers in CI need to load the call through a single point of entry file index.php (default). That is, by default, all CI development project URLs are in the form of the following:

Http://localhost/index.php/blog/logs/this_is_a_test_entry

Obviously, by default, index.php in the URL address section of the existence of a certain degree of impact on the simplicity of the URL and SEO. We can get rid of this nasty index.php by using the method described in the following article.

You may have noticed that there is already a workaround for this problem in the CodeIgniter user's 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, please confirm your host support. htaccess configuration. Among them, if Apache as a Web server, need to open the support of the Mod_rewrite module, if IIS as a Web server, you need additional installation Isapi_rewrite expansion.

The specific methods are as follows:

1. Copy and save the following configuration information as a. htaccess file.
The following are. htaccess file information

Copy 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 which 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 of the project where CI is located (that is, with the index.php sibling directory)

3. Modify the following parameters in application/config.php:

Copy Code code as follows:

$config [' index_page '] = "index.php";

To

Copy Code code as follows:

$config [' index_page '] = ""; Set to NULL

The above three steps, indispensable. If everything is configured correctly, you will find that when you run the program again, the program has been automatically hidden index.php this URL section!

Trackback (UTF-8): HTTP://WWW.CNSATURN.COM/TRACKBACK/40

The problem of mod_rewrite hiding index.php when opening path_info in CodeIgniter.

In CodeIgniter, when I change the URI addressing mode from auto to Path_info, that is:

Copy 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 method of the system.

In this case how to still use the above. htaccess scheme, the result will be: index.php smooth hide, but the master controller does not get the value correctly.

The solution is as follows, one step:

Remove the question mark that follows the index.php in the following rewrite rule.

Copy Code code as follows:

rewriterule^ (. *) $/index.php?/$1[l]

The revised rules are as follows:

Copy Code code as follows:

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

Other places are unchanged.

How to delete a index.php file

It is estimated that a lot of people learn codeigniter the first step to do is how to remove index.php, this Official handbook has related tutorials, modify. htaccess file (if your server is Apache):

Copy Code code as follows:

Rewriteengine on
Rewritecond $!^ (index\.php|images|robots\.txt)
Rewriterule ^ (. *) $/index.php/$1 [L]

Of course, there are a lot of people who have changed as required, but there have been errors, all the visits are 404, and this 404 is Apache's 404 page, not the CodeIgniter 404 error page.

This problem is not understood by the rewrite rules of Apache:

The first line, set the Rewriteengine engine to on, is to have the URL rewrite take effect;
The second line, the configuration URL rewrite rule,!^ (Index\.php|images|robots\.txt), which indicates which files need not be rewritten, but is accessed directly;
The third line, ^ (. *) $ is a regular expression, meaning that all requests are sent to the/index.php/$1, and those familiar with the URL know that the backslash (/) begins with a relative path, relative to who? The root, which is the URL.

So, if CodeIgniter is not installed in the root directory of the site, there will be errors. How to solve it, in the CodeIgniter manual also gives the corresponding solution:

Replace the last sentence with the following:

Copy 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 has been unable to see your site is developed with CodeIgniter, or ROR development.

However, how to add suffixes after the URL, so that we can even hide or fake the development language of the website, by modifying the config/config.php file, you can add a specified file suffix for the URL generated by CodeIgniter. For example, you can add. html, and even you can add. asp,.jsp.

So we can turn http://www.jb51.net/index.php/news/view/about into http://www.jb51.net/index.php/news/view/about.html.
How to use query strings

Normally we don't need to use query strings, but there are always special cases that we can't do with CodeIgniter rest mode, so we need to use the query string in the URL:

Copy Code code as follows:

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

CodeIgniter default this feature is turned off, if you want to turn on, open the configuration file application/config/config.php you can see the following:

Copy 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 the enable_query_strings to TRUE, the function is activated. At this point, you can use the keyword to invoke the desired controller and method:

Copy Code code as follows:

Index.php?c=controller&m=method

When we use CodeIgniter to make pagination, this can come in handy.

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.