CI framework. htaccess hidden URLs in index.php solution

Source: Internet
Author: User
Tags codeigniter

CodeIgniter (hereinafter referred to as "CI") is a good foreign PHP lightweight MVC framework that supports PHP4 and PHP5. is a powerful tool for developing small and medium-sized Web applications with high scalability requirements. The blog program you see Right now is written in CI.

Adhering to the idea of the MVC architecture, all controllers in CI need to be loaded into the call via a single point of entry file index.php (default).

Other words. By default, all CI development projects have URL shapes For example in the following form:

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

Very clearly. By default. index.php in the URL address section of the existence of a certain degree of impact on the URL of the concise and SEO.

We can get rid of this nasty index.php by following the methods described in this article.

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 this problem. This article now gives a more intact solution.

Note: before continuing, confirm that your host supports the. htaccess configuration. Of Assuming that Apache is a webserver, you need to turn on support for the Mod_rewrite module, and if you use IIS as a webserver, additional isapi_rewrite expansion is required.

Detailed methods such as the following:

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

The following is a. htaccess file information
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 ]      

#假设没有安装mod_rewrite模块, all 404 pages will be #发送到index. php. At this point, the program executes errordocument as if it were not set to hide 404 / index.php

2 . upload the above. htaccess file to the root folder of the project where CI is located (that is, with the index.php sibling folder)

3. change application/config.php such as the following:

1 $config [ Index_page '   =   " index.php "

To

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

Three steps above. Indispensable. Assume everything is configured properly. You will find that the time to execute the program again. The program has its own initiative to hide index.php this URL segment!

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

mod_rewrite to hide 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

$config [' Uri_protocol '] = ' path_info ';

Note: The path_info is turned on. is because I want to use $_get to get the value, not the system default post mode.

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

The solution is as follows, in one step:

Remove the question mark after index.php in the following rewrite rule.

rewriterule^ (. *) $/index.php?

/$1[L]

The changed rules are as follows:

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

The rest of the place does not change.


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


"Other" Follow me to learn the site development Framework CodeIgniter URL Chapter

How to delete index.php file

A lot of people are expected to learn CodeIgniter the first step is how to get rid of index.php, this official manual has related tutorials, change. htaccess file (if your server is Apache):

Rewriteengine on

Rewritecond $!^ (index\.php|images|robots\.txt)

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

Of course. A lot of people changed according to the requirements, but there was a mistake, all the interview is 404, and. This 404 is Apache's 404 page. Instead of the CodeIgniter 404 error page.

The problem with this is that the rewrite rules of Apache do not understand:

    • The first line, setting the Rewriteengine engine to On, is for URL rewriting to take effect.
    • The second line, configure the URL rewrite rule.!^ (Index\.php|images|robots\.txt) This statement indicates which files do not need to be rewritten, but is directly interviewed;
    • The third line, ^ (. *) $ is a regular form, meaning that all requests are sent to/index.php/$1. People familiar with the URL know that the backslash (/) begins with a relative path, relative to who? Root, which is the URL.

So, suppose CodeIgniter assumes that it is not installed in the root folder of the site, there must be an error. How to solve it, in the CodeIgniter manual also gives the corresponding solution:

Change the last sentence above to read: Rewriterule ^ (. *) $ index.php/$1 [L]

Just need to remove the index.php front slash.

How to add a URL suffix

Pass the above steps. We have hidden the index.php, and now we make the site more restful. 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 site's development language, by altering the config/config.php file, you can add a specified file suffix for the codeigniter generated URL, For example, you can join. HTML and even you can join. asp,.jsp.

So we can turn http://www.hualai.net.cn/index.php/news/view/about into a http://www.hualai.net.cn/index.php/news/. View/about.html.

How to use query strings

Normally we don't need to use a query string, but there are some special cases where we can't complete with codeigniter rest mode, so we need to use the query string in the URL:

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

CodeIgniter default This function is turned off, the false assumption is turned on. Open the configuration file application/config/config.php you can see such as the following:

$config [' enable_query_strings '] = FALSE;

$config [' controller_trigger '] = ' C '; Controller name

$config [' function_trigger '] = ' m '; Method name

$config [' Directory_trigger ']= ' d '; Name of the subfolder in which the controller resides

Suppose you change enable_query_strings to TRUE. Then this function is activated. At this point, you will be able to invoke the required controller and method through the keyword:

Index.php?c=controller&m=method

This can be useful when we are using codeigniter when paging.


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

CI framework. htaccess hidden URLs in index.php solution

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.