CodeIgniter in the framework about URLs (index.php), CodeIgniter framework _php Tutorials

Source: Internet
Author: User
Tags php framework codeigniter fast web nginx server

CodeIgniter in the framework about URLs (index.php), CodeIgniter framework


Recently, when I made my own personal website, I used the lightweight PHP framework CodeIgniter. At first glance, the code is clear and concise, and the MVC model is very easy to maintain. The tool I used to develop was NetBeans IDE 8.0, and of course, the content of this article was not related to the development tools, and it was very much related to the server we used on our last website. Currently the most commonly used two free web server is Apache and Nginx (this two server comparison, you can refer to an online classic article: http://zyan.cc/nginx_php_v6/). In my web site development and on-line process, just two servers are used, they configure codeigniter slightly different, the next separately elaborated, also in order to let other developers in the use of the CodeIgniter frame less step on the pit.

(1) About CodeIgniter

CodeIgniter is an open-source, ultra-lightweight MVC framework that is often used in fast web development, and can even change the source code to suit your needs if you want. This framework itself does not want to do more introduction, need to be familiar with friends can go to codeigniter official website to download, and the website also provides a very detailed Chinese help manual, can be very good to help new students learn. Help manual link: http://codeigniter.org.cn/user_guide/toc.html. The Help manual starts from 0 to teach you to build a simple website.

(2) Website URL of codeigniter Erection

At the beginning of the CodeIgniter site URL is this:

http://[website URL]/index.php/[controller class Name]/[class function name]/[function parameter]

Give an example: Http://127.0.0.1/index.php/welcome/hello/zhangsan. In this example, Assume that the site URL is 127.0.0.1, which is our usual native address, using the Controller folder welcome.php this PHP-written class processing this URL request, the specific method is to call the Welcome class inside the Hello function, this function is required a parameter , the argument we passed in is the string Zhangsan. However, there is a little more annoying, that is, the URL contains a fixed field index.php, looking very uncomfortable. The result I want is this: Http://127.0.0.1/welcome/hello/zhangsan. So how do you get rid of index.php? This is where I want to share the key.

Before we talk about how to get rid of index.php, let's figure out why it appears here. For any URL request, CodeIgniter is processed first by the index.php file located in the Web site and directory, which then determines which function of the class to which the request is processed, based on the index.php of the URL that you provide. Therefore, the URL must contain index.php This field, explicitly tell the server, this URL you first let index.php to redirect to the class I specified later to deal with. If you do not go through any configuration to directly remove index.php, your page is not displayed. So if we want to get rid of it, we want to go through some configuration options, let the server see a URL by default index.php to deal with it, no longer show index.php in the URL.

(3) Remove index.php under Apache server

I used the Apache server when I was developing on my own computer, so I would inevitably have to solve this problem on the Apache server first. In fact, the article at the beginning of the CodeIgniter official help manual has given the Apache solution, but did not give the solution under the Nginx. No way, Apache server is said to have more than 60% market share, as such a mainstream server, the official manual is still necessary to explain its configuration method. To be clearer, I'll explain in more detail.

Under the site root directory (that is, the same directory as mentioned earlier), create a new file named. htaccess. index.php. Be careful not to forget that there is a point in front of htaccess. Open the file with Notepad and write the following command:

%{request_filename}-%{request_filename}-Drewritecond $1 !^ (index\.php|images|js|css |  ^ (. *) $/index.php/$1 [L]

Look at the English word rewrite also guessed, in fact, this file control is the URL rewrite rules. The specific rewrite rules and all kinds of syntax for the. htaccess file are a brainiac, which is not detailed here. We are only concerned with the meaning of these words.

The first sentence rewriteengine on: translation is "rewrite engine on", equivalent to the start URL rewriting mechanism.

The second sentence Rewritecond%{request_filename}!-f: Translated is "rewrite the request file name is not a file." The last letter F, which is understood as File,!-f's exclamation point, indicates negation and is not a file.

The third sentence Rewritecond%{request_filename}!-d: Translated is "rewrite the request file name is not a directory." The last letter D is understood as directory.

The purpose of the second and third sentence is that the rewrite rules are enabled only if your URL request is not the name of the file or the name of the folder. Examples, such as http://127.0.0.1/test.html. This request is actually in the root directory of your site to find test.html, if found, you can return the file directly, do not rewrite it, only in the root directory cannot find test.html, only rewrite this URL, this is Request_filename is a file example. Again such as Http://127.0.0.1/nihao, here Nihao is likely to be a folder under the root directory (of course, can also be a file without suffix), in this case, first look for the root directory there is no Nihao this folder, no words to enable URL rewriting, This is an example of a request_filename directory.

Sentence Rewritecond $!^ (index\.php|images|js|css|robots\.txt): the first parameter after overriding the conditional URL URL cannot be index.php,images, CSS, JS, robots.txt any one ". For example, Http://127.0.0.1/images/girl.png, the first parameter of this URL is images, in which case, do not rewrite it, only if not listed above. The purpose of this sentence is to exclude some URL request rewrite, because we often put the Web site CSS files, javascript files, picture files in the root directory under the Css,js,images folder, and then in the Web page through the URL reference these resources, If the URL that requests these resources is also rewritten, the page is not referenced. You can add some additional rewrite URLs that need to be excluded, depending on your actual needs.

The sentence rewriterule ^ (. *) $/index.php/$1 [L]: Translated is "rewrite the rules to the URL URLs after the first parameter preceded by a index.php". [L] Indicates that this is the last rewrite rule, which is not followed.

This way, if you enter Http://127.0.0.1/hello/zhangsan in the browser, it is actually the equivalent of Http://127.0.0.1/index.php/hello/zhangsan.

Finally there is a small pit, is in the use of tools to develop the site, often our website code is not in the root directory of Apache server, such as we in the root directory of the Apache server and set up a folder xxx, and put the whole site in this folder, Then our homepage address is http://127.0.0.1/xxx/index.php. The above configuration file must be changed to the fifth sentence Rewriterule ^ (. *)/xxx/index.php/$1 [L], there is also a modification is to directly remove the fifth sentence in front of the index.php slash (that is, Rewriterule ^ (. *) $ index.php/$1 [L]), this is a point of attention!

After completing the above. htaccess file, there are two more things to do.

First, find CodeIgniter in the application/config/config.php file, set Index_page to NULL, that is $config[' index_page '] = ' ""; and Base_. The URL is set to the Web site root directory (the directory where index.php is located), $config [' Base_url ']= ' http://127.0.0.1/xxx/'. Deploy to a real server to make the site online before, do not forget to change 127.0.0.1 to your website URL, if index.php in the server root directory, also remember to base_url this XXX removed.

Second, find the Apache configuration file, which is conf/httpd.conf this file, make sure LoadModule rewrite_module modules/mod_rewrite.so in front of the pound # has been removed. Then keyword search htaccess, find the configuration. htaccess section, its settings should be changed to allowoverride all. In fact, if you do not use the very old version of Apache, the pound and allowoverride all should be set up by default. This step is just a confirmation, not so match to change to this.

At this point, the Apache server under the CodeIgniter URL configuration is done. Now index.php does not need to appear in the URL, the system will default to let index.php first to deal with the URL.

(4) Remove index.php under Nginx server

The above mentioned Apache removal index.php in the official help document also has a brief description, but Nginx server is not so fortunate. I was in the development of the site is Apache, but the Web site when the server is Nginx, and therefore had to go online search nginx configuration under the server, tossing a long time, tried the wrong trial many times, finally put a correct version of the trial out, and now can provide you with reference. Because of their own configuration of nginx not in-depth study, so first explain their own online server environment, and then show the changes in Nginx configuration. With my current configuration, the pro-test can work well, we have encountered a similar problem can follow my configuration to try, but I do not dare to guarantee that on your system will be effective ... My online server is buy xx cloud (Avoid advertising ~) Server (self-starting from scratch a server is too troublesome), the system is configured after the default is Nginx. The operating system is based on the Ubuntu 12.04,nginx version is nginx/1.1.19.

See many people on the Internet Nginx Server default configuration file is/etc/nginx/nginx.conf, and mine is no exception. However, there is a note, sometimes there will be a nginx.conf include * * * * (another file), that is, referring to the contents of a file outside as a configuration file, this time, If you did not find the server configuration in nginx.conf, you might want to go to another file in its include, which is my case. In my configuration file, the configuration of the server should be changed to the following:

Server {Listen the; Root/usr/share/nginx/www;        Index index.php index.html index.htm; # Make site accessible fromhttp//localhost/server_name localhost; Location/{index index.php index.html index.htm; # Uncomment to enable Naxsi on ThisLocation # include/etc/nginx/Naxsi.rules # Please note the following rewrite rule, which is similar to the one in front of Apacheif(!-e $request _filename) {# #如果没有找到目标文件 rewrite^/(. *) $/index.php/$1Last ;  Break; The rewrite rules above can also be changed to the following form, both of which are feasible in the pro-Test #if($request _filename!~ (js|styles|images|robots\.txt|index\.php.*)) {# #如果不是请求js, styles and other files
# rewrite^/(. *) $/index.php/$1Last ;
# Break;
#}} Location/doc/{alias/usr/share/doc/; AutoIndex on; allow127.0.0.1; Deny all; } location ~ \.php ($|/){fastcgi_split_path_info^ (. +\.php) (. *)$; Fastcgi_pass127.0.0.1:9000; Fastcgi_index index.php; Fastcgi_param path_info $fastcgi _path_info; Fastcgi_param script_filename $document _root$fastcgi_script_name; Include Fastcgi_params; } # Deny access to. htaccess files,ifApache's Document Root# concurs with Nginx'S One# Location~ /\.ht {deny all; }}

The specific changes have been marked in the comments above, very simple rewrite rules, I have been tossing a long time. Hope to share out, help everyone less step on the pit. Just write it down here!


How to get the URL of the current page in PHP CodeIgniter framework

Use URL Helper
$this->load->helper (' url ');
Current_url = Current_url ();

How to delete the CodeIgniter frame in the URL segment inside the indexphp, instance, the manual on the wood to see clearly, I wrote a dome test is not

CodeIgniter is a lightweight, fast, PHPMVC framework that generates very clean and SEO-friendly URLs, and by default, index.php files will be included in your URL for example: example.com/index.php/ News/article/my_article first make sure your Apache server supports mod_rewrite, mod_rewrite need to modify httpd.conf, remove LoadModule rewrite_module Modules/mod_rewrite.so before the # you can easily pass the. htaccess file to set some simple rules to delete it. The following is an example of using the "negative" method to redirect non-specified content: Rewriteengine onrewriterule ^ (. *) $/index.php/$1 [L] If your project is not in the root directory, please change the above sentence to: rewriterule ^ (. *) $ index.php/$1 [L] then put the file in the root directory of the site in the example above, you can implement any non-index.php, images and robots.txt HTTP requests are pointed to index.php. Note Modify the configuration of the file based on the location of your project. Setting allowoverride to none can completely prohibit the use of. htaccess files, note that your Apache configuration file allowoverride is set to all

http://www.bkjia.com/PHPjc/880091.html www.bkjia.com true http://www.bkjia.com/PHPjc/880091.html techarticle CodeIgniter in the framework of the things about URLs (index.php), the CodeIgniter framework recently, when making its own personal site, adopted a lightweight PHP framework codeigniter. At first glance, a generation of ...

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