thinkphp3.2 nginx环境 隐藏 index.php

本文详细解析了Nginx配置中的try_files指令作用,通过实例展示了如何设置重定向及错误页面,适用于PHP环境下的URL重写,确保静态资源与动态请求的正确处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先config.php中URL_MODEL = 2   

VHOST loaction / {

添加   try_files $ uri $ uri / /index.php?s=$uri&$args;

}

意思是:如果第一个$ URI不存在,就访问$ URI /;如果$ URI /还不存在,访问/index.php?s=$uri&$args可以后面跟很多个。

try_files从字面上理解就是尝试文件,再结合环境理解就是“尝试读取文件”,那他想读取什么文件呢,

答:读取静态文件

 

$ uri这个是nginx的一个变量,存放着用户访问的地址,

比如:http://www.xxx.com/index.html,那么$ uri就是/index.html

 

$ uri /代表访问的是一个目录,比如:http://www.xxx.com/hello/test/,那么$ uri /就是/ hello / test /

 

完整的解释就是:try_files去尝试到网站目录读取用户访问的文件,如果第一个变量存在,就直接返回;

不存在继续读取第二个变量,如果存在,直接返回;不存在直接跳转到第三个参数上。

 

再例如:

1

try_files $uri = 404

什么意思呢?URI不能成功访问,那好,那就给你个404吧。

 

服务器{
        listen 80;
        server_name www.search.com;
        root“F:\ PHPTutorial \ WWW \ CRM-Search \ Public”;
        location / {
        try_files $ uri $ uri / /index.php?s=$uri&$args;
            index index.php;
            #autoindex on;
        }
        location~ \ .php(。*)$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_split_path_info ^((?U)。+ \ .php)(/?。+)$;
            fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
            fastcgi_param PATH_INFO $ fastcgi_path_info;
            fastcgi_param PATH_TRANSLATED $ document_root $ fastcgi_path_info;
            包括fastcgi_params;
        }
}

 

加这个 也 可以的

   if (!-e $request_filename)
            {
                rewrite ^(.*)$ /index.php?s=$1 last; break;
            }

 

 

server {
        listen       80;
        server_name  www.rights.com ;
        root   "F:\PHPTutorial\WWW\api_rights\public";
    index index.php index.html index.htm;
     sendfile off;
        location / {
        if (!-e $request_filename){
            rewrite  ^(.*)$  /index.php?s=$1 last;
            break;
        }
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}server {
        listen       80;
        server_name  www.rights.com ;
        root   "F:\PHPTutorial\WWW\api_rights\public";
    index index.php index.html index.htm;
     sendfile off;
        location / {
        if (!-e $request_filename){
            rewrite  ^(.*)$  /index.php?s=$1 last;
            break;
        }
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

ThinkPHP 3.2.3中,可以通过修改.htaccess文件或者在Apache配置文件中增加重写规则来实现隐藏index.php和Home。 方法一:修改.htaccess文件 1. 首先,确认你的服务器支持.htaccess文件,并且.htaccess文件已经开启。 2. 在项目根目录下新建.htaccess文件,并添加以下内容: ``` RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] ``` 3. 保存并上传.htaccess文件到服务器根目录。 4. 修改配置文件config.php,将URL_MODEL参数改为2。 ``` &#39;URL_MODEL&#39; => 2, ``` 5. 访问项目时,可以通过访问http://域名/控制器/方法 的方式来访问,而不需要在URL中加上index.php或Home。 方法二:在Apache配置文件中增加重写规则 1. 打开Apache配置文件httpd.conf,搜索LoadModule rewrite_module modules/mod_rewrite.so,并确保该模块已开启。 2. 在VirtualHost标签中添加以下内容: ``` <Directory "/var/www/html"> Options FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> ``` 其中/var/www/html为项目根目录,AllowOverride All表示允许.htaccess文件中的重写规则生效。 3..htaccess文件中添加以下内容: ``` RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] ``` 4. 修改配置文件config.php,将URL_MODEL参数改为2。 ``` &#39;URL_MODEL&#39; => 2, ``` 5. 访问项目时,可以通过访问http://域名/控制器/方法 的方式来访问,而不需要在URL中加上index.php或Home。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值