Shows you how to use.htaccess in the CI framework to hide index.php in url

  • 2021-06-28 11:59:13
  • OfStack

Adhering to the idea of MVC architecture, all controllers in CI need to load calls through the single entry file index.php (default).That is, by default, all CI development projects have the following URLs:

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 URL and the progress of SEO to some extent.We can get rid of this nasty Index.php by following the methods described below.

You may have noticed that solutions to this problem already exist in the CodeIgniter user manual.But this officially provided.htaccess configuration does not always solve the problem.This article now gives a more complete solution.

Note: Before proceeding, make sure your host supports the.htaccess configuration.Where, if Apache is an Web server, mod_needs to be turned onSupport for the rewrite module;Additional ISAPI_installation is required if IIS is used as the Web serverRewrite Extension.

The specific methods are as follows:

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


RewriteEngineOn
RewriteBase /  
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule^(.*)$ /index.php?/$1 [L]      
# If not installed mod_rewrite Module, all 404 All pages will be  # Send to index.php At this point, the program looks like it was not hidden 1 Sample Run    
ErrorDocument404 /index.php

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

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


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

to


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

Three steps above, one is 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

Open PATH_in CodeIgnitermod_at INFOrewrite hides the problem with index.php.

In CodeIgniter, when I changed the URI addressing style from AUTO to PATH_When INFO, that is:


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

Note: PATH_INFO was started because I wanted to pass $_GET takes the value instead of the system default POST mode.

In this case, how to still use the above.htaccess scheme, the result will be: index.php is hidden smoothly, but the master controller can not get the value correctly.

The solution is as follows: Step 1:

Remove the question mark following index.php from the rewrite rule below.


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

The modified rules are as follows:


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

Other places remain unchanged.

How to delete the index.php file

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


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

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

This problem arises when the rewrite rule of apache is not understood:

Line 1, setting the RewriteEngine engine to on, makes the url override take effect;
Line 2, Configure url Rewrite Rule,!^(The regular expression indexphp|images|robotstxt) indicates which files do not need to be overridden but are accessed directly;
Line 3, ^(. *)$is a regular expression, meaning that all requests are sent to/index.php/$1. Anyone familiar with url knows that starting with a backslash (/) is a relative path, relative to whom?The root is the web address.

So, if CodeIgniter is not installed in the root directory of the website, there will be errors.How to solve this problem is also given in the CodeIgniter manual:

Change the last sentence above to:


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

Just remove the slash in front of index.php.

How to add the url suffix

With the steps above, we have hidden index.php. Now we have made a website with more rest. The average person can no longer see at a glance whether your website was developed with CodeIgniter or ROR.

However, how to add a suffix after url, so that we can even hide or fake the website's development language. By modifying the config/config.php file, you can add a specified file suffix to URL generated by CodeIgniter, such as. html, or even.asp,.jsp.

In this way, we can turn https://www.ofstack.com/index.php/news/view/about into https://www.ofstack.com/index.php/news/view/about.html.
How to use query strings

1 Normally we don't need to use query strings, but there are always special cases where we can't do this with rest mode of CodeIgniter, so we need to use query strings in URL:


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

CodeIgniter turns this off by default. If you want to turn it on, open the configuration file application/config/config.php and 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'; // The name of the subdirectory where the controller resides 

If you will enable_query_When strings is changed to TRUE, this function is activated.At this point, you can use keywords to invoke the desired controller and method:


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

This comes in handy when we use CodeIgniter to make paging.


Related articles: