What about URL (index. php) in the CodeIgniter framework?

Source: Internet
Author: User
Tags nginx server

What about URL (index. php) in the CodeIgniter framework?

Recently, CodeIgniter, a lightweight php framework, was used to build a personal website. At first glance, the code is clear and concise, and the MVC model is very easy to maintain. The tool I used during development is Netbeans IDE 8.0. Of course, the content in this article has nothing to do with the development tool, and it has a lot to do with the server we used on our last website. Currently the two most commonly used free web servers are Apache and Nginx (comparison of these two servers, you can refer to a classic online article: http://zyan.cc/nginx_php_v6 ). During the development and launch of my website, the two servers were used. They were slightly different in CodeIgniter configuration. Next, I will explain them separately, to prevent other developers from using the CodeIgniter framework.

 

(1) about CodeIgniter

CodeIgniter is an open-source ultra-lightweight MVC framework. It is often used in rapid Web development. If you want to, you can even change the source code to meet your needs. This framework does not need to be introduced much. If you need to be familiar with it, you can download it from the official CodeIgniter website. The official website also provides a very detailed Chinese help manual to help new users learn. Help manual link: http://codeigniter.org.cn/user_guide/toc.html. The help manual starts from 0 to teach you how to build a simple website.

 

(2) CodeIgniter website URL

The website URL set up by CodeIgniter is like this:

Http: // [website url]/index. php/[controller class name]/[class function name]/[function parameter]

For example, http: // 127.0.0.1/index. php/welcome/hello/zhangsan. In this example, assume that the website URL is 127.0.0.1, that is, our common local address, using the welcome in the controller folder. the php class is written in php to process this url request. The specific processing method is to call the hello function in the welcome class. This function requires a parameter, the input parameter is the string zhangsan. However, the url contains a fixed field index. php, which looks uncomfortable. The result is as follows: http: // 127.0.0.1/welcome/hello/zhangsan. So how to remove index. php? This is what I want to share with you.

 

Before explaining how to remove index. php, let's figure out why it appears here. For any url request, CodeIgniter is first located under the website and directory. PHP file processing, this file is based on the index in the url you provide. php is used to determine which function of the class the request is handed over. Therefore, the url must contain the index. php field, which explicitly tells the server that index. php will redirect the url to the class I specified later for processing. If index. php is removed without any configuration, Your webpage cannot be displayed. If we want to remove it, we want to go through some configuration options to let the server see a url and use index by default. php just needs to handle it, so you don't need to set the index. php is displayed in the url.

 

(3) Remove index. php from the Apache server

I use the Apache server for development on my computer, so it is inevitable to solve this problem on the Apache server first. In fact, the CodeIgniter official help manual mentioned at the beginning of this article has already provided a solution for Apache, but has not provided a solution for Nginx. No way. It is said that the market share of Apache servers has exceeded 60%. As such a mainstream server, the official manual still needs to describe its configuration method. To be clearer, I will explain it in more detail.

 

Create a file named. htaccess under the root directory of the website (that is, the same directory as index. php mentioned above. Do not forget that there is another point in front of htaccess. Open the file in notepad and write the following command:

RewriteEngine onRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteCond $1 !^(index\.php|images|js|css|robots\.txt)RewriteRule ^(.*)$ /index.php/$1 [L]

Let's take a look at the English word Rewrite. In fact, this file controls the url Rewrite rules. The specific rewriting rules of. htaccess files and the syntax of all types are also a university question, which will not be detailed here. We only care about what these words mean.

The first RewriteEngine on is translated as "rewrite engine enabled", which is equivalent to starting the url rewrite mechanism.

RewriteCond % {REQUEST_FILENAME }! -F: the translation is "rewrite condition request file name is not a file ". The last letter f is interpreted as file ,! -F's exclamation point indicates no, indicating that it is not a file.

Third sentence RewriteCond % {REQUEST_FILENAME }! -D: "The rewrite condition request file name is not a directory ". The last letter d is interpreted as directory.

The purpose of the second and third sentences is to enable the rewrite rule only when your URL request is not the file name or folder name. For example, http: // 127.0.0.1/test.html. This url is rewritten only when the url is renewed. This is an example of REQUEST_FILENAME being a file. Another example is http: // 127.0.0.1/nihao. Here, nihao is probably a folder in the root directory (of course, it can also be a file without a suffix ), in this case, check whether the root directory contains the nihao folder. If not, enable url rewriting. This is an example of REQUEST_FILENAME as a directory.

Fourth sentence RewriteCond $1! ^ (Index \. php | images | js | css | robots \. txt): "The first parameter after the url rewrite condition cannot be index. php, images, css, js, and robots.txt ". For example, http: // 127.0.0.1/images/girl.png. The first parameter of this url is images. In this case, do not rewrite the url. Only those listed above are overwritten. The purpose of this sentence is to exclude the rewriting of some url requests, because we often put the css files, javascript files, and image files of the website in the css, js, and images folders under the root directory, then, these resources are referenced through URLs on the webpage. If the URLs requested for these resources are also overwritten, they will not be referenced in the webpage. You can add new rewrite URLs that need to be excluded based on your actual needs.

RewriteRule ^ (. *) $/index. php/$1 [L ". [L] indicates that this is the last rewrite rule, and it will not be followed.

In this way, if you enter http: // 127.0.0.1/hello/zhangsan in your browser, it is equivalent to http: // 127.0.0.1/index. php/hello/zhangsan.

There is also a small pitfall, that is, when using tools to develop websites, our website code is often not under the root directory of the Apache server, for example, if we create another folder xxx under the root directory of the Apache server and put the entire website in this folder, then our home address is http: // 127.0.0.1/xxx/index. php. In this case, you must change the fifth sentence of the above configuration file to RewriteRule ^ (. *)/xxx/index. php/$1 [L]. Another way is to directly remove the index in the fifth sentence. the forward slash (RewriteRule ^ (. *) $ index. php/$1 [L]). Pay attention to this!

 

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

First, find application/config in CodeIgniter. PHP file, set index_page to null, that is, $ config ['index _ page'] = '""; and base_url to the website root directory (index. php Directory), $ config ['base _ url'] = "http: // 127.0.0.1/xxx /". Before deploying your website to a real server, do not forget to change 127.0.0.1 to your website URL. If index. php is placed in the root directory of the server, remember to remove xxx from base_url.

Second, find the Apache configuration file, that is, conf/httpd. conf, and ensure that the # In Front Of LoadModule rewrite_module modules/mod_rewrite.so has been removed. Search for htaccess by keyword, find the configuration of. htaccess, and set it to AllowOverride All. In fact, if you do not use the old version of Apache, the well number and AllowOverride All should be set by default. This step is just to confirm that it should not be changed to this.

 

Now, CodeIgniter URL configuration in Apache server is complete. Now index. php does not need to appear in the URL. The system will first let index. php handle the URL by default.

 

(4) Remove index. php from the Nginx Server

The official help documentation for removing index. php from Apache mentioned above also provides a brief description, but the Nginx server is not so lucky. I used Apache locally during website development, but Nginx was the server when the website went online. Therefore, I had to search for the configuration under the Nginx server online for a long time, I tried it many times and finally tried a correct version. Now I can provide it for your reference. Because I have not studied Nginx configuration in depth, I should first describe the environment for my online server and then demonstrate the modification content of Nginx configuration. With my current configuration, the test can work well. If you encounter a similar problem, you can try it according to my configuration, but I am not sure it will work on your system ...... My online server is XX cloud (avoid advertisement ~) Server (it is too troublesome to create a server from scratch). After the system is configured, Nginx is used by default. The operating system uses Ubuntu 12.04 and Nginx version nginx/1.1.19.

 

The default configuration file for many Nginx servers on the Internet is/etc/nginx. conf, which is no exception to me. But there is a note that sometimes nginx. conf contains an include *** (another file), that is, referencing the content of an external file as the configuration file. At this time, if you are not in nginx. find the server-related configuration in the conf file and find it in another file included. This is my case. In my configuration file, the server-related configuration should be changed to the following:

Server {listen 80; root/usr/share/nginx/www; index. php index.html index.htm; # Make site accessible from http: // localhost/server_name localhost; location/{index. php index.html index.htm; # Uncomment to enable naxsi on this location # include/etc/nginx/naxsi. rules # pay attention to the following rewrite rule, which is similar to the previous Apache if (! -E $ request_filename) {## if the target file rewrite ^ /(. *) $/index. php/$1 last; break;} # The above rewrite rules can also be changed to the following form, which is feasible for both tests # if ($ request_filename! ~ (Js | styles | images | robots \. txt | index \. php. *) {## if you do not request files such as js and styles
# Rewrite ^/(. *) $/index. php/$1 last;
# Break;
#}} Location/doc/{alias/usr/share/doc/; autoindex on; allow 127.0.0.1; deny all;} location ~ \. Php ($ |/) {fastcgi_split_path_info ^ (. + \. php )(. *) $; fastcgi_pass 127.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. htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\. Ht {deny all ;}}

The specific changes have been marked in the above annotations. I have been tossing for a long time to rewrite the rules. I hope to share it with you to help you avoid difficulties. Write it here!

 


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

// Use url helper
$ This-> load-> helper ('url ');
Current_url = current_url ();
 
How can I delete the indexphp, instance in the URL segment of the CodeIgniter framework? I can see from the manual that I wrote a dome test.

CodeIgniter is a lightweight and fast PHPMVC framework that can generate very clean and user-friendly URLs. By default, index. the php file will be included in your URL, for example: example.com/index.php/news/article/my_articlefirst, ensure that your apacheserver supports mod_rewrite. To enable mod_rewrite, You need to modify httpd. conf, remove the LoadModule rewrite_module modules/mod_rewrite.so before # You can easily pass. htaccess file to set some simple rules to delete it. The following example uses the "negative" method to redirect unspecified content: RewriteEngine onRewriteRule ^ (. *) $/index. php/$1 [L] If your project is not in the root directory, change the above sentence to RewriteRule ^ (. *) $ index. php/$1 [L] Then put the file in the root directory of the website. In the preceding example, any non-index can be implemented. HTTP requests for php, images, and robots.txt are directed to the index. php. Modify the file configuration according to the location of your project. Setting AllowOverride to none can completely disable the use of the. htaccess file. Be sure to set your Apache configuration file AllowOverride to All

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.