Home  >  Article  >  Backend Development  >  Teach you how to use .htaccess in CI framework to hide index.php in URL_PHP tutorial

Teach you how to use .htaccess in CI framework to hide index.php in URL_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:28:45757browse

Adhering to the idea of ​​MVC architecture, all controllers in CI need to be loaded and called through the single point entry file index.php (default). That is to say, by default, the URLs of all CI development projects are in the following form:

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

Obviously, by default, the existence of index.php in the URL address segment affects the simplicity of the URL and the progress of SEO to a certain extent. We can remove this nasty Index.php through the method introduced in this article.

You may have noticed that there is a solution to this problem in the CodeIgniter user manual. However, the officially provided .htaccess configuration does not solve the problem all the time. This article now gives a more complete solution.

Note: Before proceeding, please confirm that your host supports .htaccess configuration. Among them, if Apache is used as a web server, the mod_rewrite module needs to be enabled; if IIS is used as a web server, the ISAPI_Rewrite extension needs to be installed additionally.

The specific method is as follows:

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

Copy the code 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 404 pages will be #sent to index.php. At this time, the program will run as if no hiding is set.
ErrorDocument404 /index.php

2. Upload the above .htaccess file to the root directory of the project where CI is located (i.e., the same level directory as index.php)

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

Copy code The code is as follows:

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

to

Copy code The code is as follows:

$config['index_page'] = ""; //Set to empty

The above three steps are indispensable. If everything is configured normally, 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

The problem with mod_rewrite hiding 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 code The code is as follows:

$config['uri_protocol'] = 'PATH_INFO';

Note: PATH_INFO is turned on because I want to get the value through $_GET instead of the system default POST method.

In this case, if you still use the above .htaccess solution, the result will be: index.php is successfully hidden, but the main controller cannot obtain the value correctly.

The solution is as follows, just one step:

Just remove the question mark after index.php in the following rewrite rules.

Copy code The code is as follows:

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

The modified rules are as follows:

Copy code The code is as follows:

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

Other places remain unchanged.

How to delete index.php file

It is estimated that the first step that many people want to do when learning CodeIgniter is how to remove index.php. This official manual has related tutorials, modifying the .htaccess file (provided that your server is apache):

Copy code The code is as follows:

RewriteEngine on
RewriteCond $1 !^(index.php|images|robots. txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Of course, many people have modified it as required, but errors have occurred. All visits have been 404, and this 404 is the 404 page of apache, not the 404 error page of CodeIgniter.

This kind of problem occurs because you don’t understand apache’s rewrite rules:

The first line, set the RewriteEngine engine to on, to make url rewriting take effect;
The second line, configure the url rewrite rules, !^(index.php|images|robots.txt) this regular expression The formula indicates which files do not need to be rewritten, but are accessed directly;
The third line, ^(.*)$ is a regular expression, which means that all requests are sent to /index.php/$1, familiar Everyone who knows URLs knows that anything starting with a backslash (/) is a relative path. Who is it relative to? The root is the URL.

So, if CodeIgniter is not installed in the root directory of the website, an error will inevitably occur. How to solve it? The corresponding solution is also given in the CodeIgniter manual:

Change the last sentence above to:

Copy the code The code is as follows:

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

Just remove the slash in front of index.php.

How to add url suffix

Through the above steps, we have hidden index.php. Now the website we made is more restful. Most people can no longer tell at a glance whether your website was developed with CodeIgniter or ROR.

But how to add a suffix after the URL? In this way, we can even hide or forge the development language of the website. By modifying the config/config.php file, you can add a specified file suffix to the URL generated by CodeIgniter, such as You can add .html, you can even add .asp, .jsp.

In this way we can change 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

Generally, we don’t need to use query strings, but there are always some special cases that we cannot accomplish using CodeIgniter’s rest mode, so we need to use query strings in the URL:

Copy code The code is as follows:

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

This feature of CodeIgniter is turned off by default. If you want to turn it on, open the configuration file application/config/config.php and you can see the following content:

Copy code 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'; //Controller The name of the subdirectory

If you change enable_query_strings to TRUE , then this feature is activated. At this point, you can call the required controllers and methods through keywords:

Copy code The code is as follows:

index.php?c=controller&m=method

This can come in handy when we use CodeIgniter to create pagination.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/781032.htmlTechArticleAdhering to the idea of ​​MVC architecture, all controllers in CI need to go through the single point entry file index.php (default ) to load the call. That is to say, by default, all CI development projects...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn