Things about URL (index.php) in the CodeIgniter framework, codeigniter framework_PHP tutorial

WBOY
Release: 2016-07-13 10:18:49
Original
991 people have browsed it

Things about URL (index.php) in the CodeIgniter framework, codeigniter framework

Recently, when making 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 during development was Netbeans IDE 8.0. Of course, the content of this article has nothing to do with the development tools, and has a lot to do with the server used in our final website. The two most commonly used free web servers at present are Apache and Nginx (for a comparison of these two servers, you can refer to a classic article on the Internet: http://zyan.cc/nginx_php_v6/). During the development and launch of my website, both servers happened to be used. There were slight differences in how they configured CodeIgniter. Next, I will explain them separately, so that other developers can avoid pitfalls when using the CodeIgniter framework.

(1) About CodeIgniter

CodeIgniter is an open source, ultra-lightweight MVC framework that is often used for rapid web development. If you are willing, you can even change the source code at will to suit your needs. I don’t want to introduce the framework itself. Friends who need to be familiar with it can go to the CodeIgniter official website to download it. The official website also provides a very detailed Chinese help manual, which can very well help newcomers learn. Help manual link: http://codeigniter.org.cn/user_guide/toc.html. The help manual teaches you how to build a simple website from scratch.

(2) Website URL set up by CodeIgniter

The website URL initially built by CodeIgniter looked like this:

http://[website address]/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, it is assumed that the URL of the website is 127.0.0.1, which is our commonly used local address. The welcome.php class written in PHP in the controller folder is used to process this URL request. The specific processing method is to call this welcome The hello function in the class requires a parameter. The parameter we pass in is the string zhangsan. However, one thing that is more eye-catching is that the URL contains a fixed field index.php, which looks very unpleasant. The result I want is this: http://127.0.0.1/welcome/hello/zhangsan. So how to remove index.php? This is where I mainly want to share.

Before we talk about how to remove index.php, let’s first figure out why it appears here. For any URL request, CodeIgniter is first processed by the index.php file located in the website and directory. This file then determines which class to redirect the request to based on the part after index.php in the URL you provided. Which function handles. Therefore, the URL must contain the index.php field, explicitly telling the server that you first let index.php redirect this URL to the class I specified later for processing. If you remove index.php directly without any configuration, your web page will not be displayed. So if we want to get rid of it, we hope that through some configuration options, the server will use index.php to process a URL by default, without having to display index.php in the URL.

(3) Remove index.php from the Apache server

I use the Apache server when developing on my computer, so it is inevitable to solve this problem under the Apache server first. In fact, the CodeIgniter official help manual mentioned at the beginning of the article already provides a solution under Apache, but does not provide a solution under Nginx. No way, the Apache server is said to have a market share of over 60%. As such a mainstream server, it is necessary for the official manual to explain its configuration method. To make it clearer, let me explain it in more detail.

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

<span>RewriteEngine on

RewriteCond </span>%{REQUEST_FILENAME} !-<span>f
RewriteCond </span>%{REQUEST_FILENAME} !-<span>d

RewriteCond $</span><span>1</span> !^(index\.php|images|js|css|<span>robots\.txt)
RewriteRule </span>^(.*)$ /index.php/$<span>1</span> [L]
Copy after login

Look at the English word Rewrite and you can guess it. In fact, this file controls the rewriting rules of the URL. The specific rewriting rules and all types of syntax for .htaccess files are another subject and will not be discussed in detail here. We only care about what the above sentences mean.

The first sentence RewriteEngine on: Translated is "rewrite engine on", which is equivalent to starting the url rewriting mechanism.

The second sentence RewriteCond %{REQUEST_FILENAME} !-f: Translated is "Rewrite condition request file name is not a file". The last letter f is understood as file, and the exclamation mark of !-f means negation, which means it is not a file.

The third sentence RewriteCond %{REQUEST_FILENAME} !-d: Translated is "Rewrite condition request file name is not a directory". The last letter d is understood as directory.

The purpose of the second and third sentences is to enable the rewrite rule only when your URL request is not the name of a file or a folder name. For example, take http://127.0.0.1/test.html. This request is actually to search for test.html in the root directory of your website. If it is found, just return this file directly without rewriting; only when test.html cannot be found in the root directory, the url will be rewritten. This is an example where REQUEST_FILENAME is a file. Another example is http://127.0.0.1/nihao. Here nihao is most likely a folder in the root directory (of course it can also be a file without a suffix). In this case, first look for the file nihao in the root directory. folder, if not, enable url rewriting. This is an example where REQUEST_FILENAME is a directory.

The fourth sentence RewriteCond $1 !^(index.php|images|js|css|robots.txt): Translated is "The first parameter after rewriting the conditional 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 this case, do not rewrite it. Only rewrite it if it is not the ones listed above. The purpose of this sentence is to exclude the rewriting of some url requests, because we often put the website’s css files, javascript files, and image files in the css, js, and images folders in the root directory, and then reference them through urls in the web page For these resources, if the URLs used to request these resources are also rewritten, they will not be referenced in the web page. You can add some new URL rewriting situations that need to be excluded based on your actual needs.

The fifth sentence RewriteRule ^(.*)$ /index.php/$1 [L]: Translated is "Rewrite rule. Add index.php in front of the first parameter after the url address". [L] indicates that this is the last rewrite rule and there will be no more.

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

Finally, there is a small pitfall. When using tools to develop websites, often our website code is not in the root directory of the Apache server. For example, we create a folder xxx in the root directory of the Apache server and put The entire website is placed in this folder, so our homepage address is http://127.0.0.1/xxx/index.php. At this time, you must change the fifth sentence of the above configuration file to RewriteRule ^(.*) /xxx/index.php/$1 [L]. Another way to change it is to directly remove the slash in front of index.php in the fifth sentence. (i.e. RewriteRule ^(.*)$ index.php/$1 [L]), please pay attention to this!

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

First, find the application/config/config.php file in CodeIgniter, set index_page to a null value, that is, $config['index_page'] = '""; and set base_url to the website root directory (index. php directory), $config['base_url']="http://127.0.0.1/xxx/". Before deploying to a real server to bring the website online, don’t forget to change 127.0.0.1 to the URL of your website. If index.php is placed in the root directory of the server, remember to remove the xxx in base_url.

Second, find the Apache configuration file, which is the conf/httpd.conf file, and make sure that the # sign in front of LoadModule rewrite_module modules/mod_rewrite.so has been removed. Then search for htaccess with keywords and find the part that configures .htaccess. Its setting should be changed to AllowOverride All. In fact, if you are not using a very old version of Apache, the pound sign and AllowOverride All should be set by default. This step is just to confirm, if it is not matched like this, it needs to be changed to this.

At this point, the URL configuration of CodeIgniter under the Apache server is complete. Now index.php no longer needs to appear in the URL. The system will default to index.php processing the URL first.

(4) Remove index.php from the Nginx server

The removal of index.php from Apache mentioned above is also briefly explained in the official help document, but the Nginx server is not so lucky. When I was developing the website, I used Apache locally, but when the website went online, the server was Nginx. So I had no choice but to go online to search for the configuration of the Nginx server. After a long time and many trials and errors, I finally got a correct version. It’s out and can now be provided for your reference. Because I have not studied the configuration of Nginx in depth, I will first explain the environment used by my online server, and then show the modifications to the Nginx configuration. With my current configuration, it can work very well in personal testing. If you encounter similar problems, you can try it according to my configuration, but I can't guarantee that it will work on your system... My online server is I bought the XX Cloud (avoid ads~) server (it is too troublesome to build a server from scratch). After the system is configured, it defaults to Nginx. The operating system is Ubuntu 12.04, and the Nginx version is nginx/1.1.19.

看网上好多人的Nginx服务器默认配置文件是/etc/nginx/nginx.conf,我的也不例外。不过有个注意事项,有时候nginx.conf中会有一句include ***(另外一个文件),也就是引用外边某个文件的内容作为配置文件,这时候,如果你没有在nginx.conf中找到服务器server相关配置,不妨去它include的另外一个文件中找一下,我的就是这种情况。在我的配置文件中,和服务器有关的配置应该改成如下:

<span>server {
        listen   </span><span>80</span><span>;

        root </span>/usr/share/nginx/<span>www;
        index index.php index.html index.htm;

        # Make site accessible </span><span>from</span> http:<span>//</span><span>localhost/</span>
<span>        server_name localhost;

        location </span>/<span> {
                index index.php index.html index.htm;
                
                # Uncomment to enable naxsi on </span><span>this</span><span> location
                # include </span>/etc/nginx/<span>naxsi.rules

                # 请留意下面这条重写规则,和前面的Apache有些类似
                </span><span>if</span> (!-<span>e $request_filename) { ##如果没有找到目标文件
                        rewrite </span>^/(.*)$ /index.php/$<span>1</span><span> last;
                        </span><span>break</span><span>;
                }
          
           
          # 上面的重写规则也可以改成下面这种形式,亲测两者都可行
          #</span><span> if</span> ($request_filename !~ (js|styles|images|robots\.txt|index\.php.*)){ ##如果不是请求js,styles等文件<br /><span>          #        rewrite </span>^/(.*)$ /index.php/$<span>1</span><span> last;<br />          #        </span><span>break</span><span>;<br />          # }
        }

        location </span>/doc/<span> {
                alias </span>/usr/share/doc/<span>;
                autoindex on;
                allow </span><span>127.0</span>.<span>0.1</span><span>;
                deny all;
        }
 
        location ~ \.php($|/)</span><span> {
                fastcgi_split_path_info </span>^(.+\.php)(.*<span>)$;
 
                fastcgi_pass </span><span>127.0</span>.<span>0.1</span>:<span>9000</span><span>;
      
                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, </span><span>if</span> Apache<span>'</span><span>s document root</span>
        # concurs with nginx<span>'</span><span>s one</span>
<span>        #
        location </span>~ /<span>\.ht {
                deny all;
        }
}</span>
Copy after login

具体的改动已经标注在上面的注释中了,很简单的一句重写规则,我却折腾了蛮久的时间。希望分享出来,帮助大家少踩坑。就写到这里吧!

 

PHP CodeIgniter框架中怎获取当前页面的URL

//use url helper
$this->load->helper('url');
current_url = current_url();
 

怎删除CodeIgniter框架中URL段里面的indexphp,实例,手册上的木看明白,自己写了个dome测试也不对

CodeIgniter 是一个轻量级的,快速的,能生成非常干净而且是对搜索引擎友好化的URL的PHPMVC框架,默认情况下,index.php 文件将被包含在你的 URL 中 例如:example.com/index.php/news/article/my_article首先确保你的Apache服务器支持支持 mod_rewrite,开启mod_rewrite需要修改httpd.conf,去掉LoadModule rewrite_module modules/mod_rewrite.so前的#你可以很容易的通过 .htaccess 文件来设置一些简单的规则删除它。下面是一个例子,使用“negative”方法将非指定内容进行重定向:RewriteEngine onRewriteRule ^(.*)$ /index.php/$1 [L]如果你的项目不在根目录请把上面这一句改为:RewriteRule ^(.*)$ index.php/$1 [L]然后将该文件放在网站的根目录在上面的例子中,可以实现任何非 index.php、images 和 robots.txt 的 HTTP 请求都被指向 index.php。注意根据你项目的位置修改文件的配置。将AllowOverride设置为none可以完全禁止使用.htaccess文件,注意将你Apache的配置文件AllowOverride设置成All
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/880091.htmlTechArticleCodeIgniter框架中关于URL(index.php)的那些事,codeigniter框架 最近,在做自己的个人网站时,采用了轻量级的php框架CodeIgniter。乍一看上去,代...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template