CI在Nginx服务器上rewrite去掉index.php例子

作者:袖梨 2022-06-25

CI框架在nginx服务器上配置rewrite去掉index.php的方法:

vim /usr/local/webserver/nginx/conf/nginx.conf
实例配置代码:

server
{
listen 80;
server_name www.111com.net;
index index.html index.htm index.php;
root /data0/htdocs/lamp100;

#nginx去掉index.php
location / {
rewrite ^/$ /index.php last;

rewrite ^/(?!index.php|robots.txt|uploadedImages|resource|images|js|css|styles|static)(.*)$ /index.php/$1 last;
}

#nginx模拟pathinfo,否则CI框架的控制器无法访问

location ~ ^(.+.php)(.*)$
{

fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+.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;

}

log_format lamp100logs '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /data1/logs/lamp100logs.log lamp100logs;

}

以上都是自己实际中在使用的。【关于$document_root可能出现file not found的话,就把那个直接换成网站的root根路径】

补充: apache配置rewrite规则,删除url中的index.php

1,在httpd.conf文件中配置如下部分


DocumentRoot D:/Source/v5

改成


DocumentRoot D:/Source/v5
# Turn on URL rewriting
RewriteEngine On

RewriteRule ^/app/(.*)$ /index.php/app/$1 [L,NC]
RewriteRule ^/api/(.*)$ /index.php/api/$1 [L,NC]
RewriteRule ^/open/(.*)$ /index.php/open/$1 [L,NC]
RewriteRule ^/admin/(.*)$ /index.php/admin/$1 [L,NC]

RewriteRule ^/app/(.*)$ /index.php/app/$1 [L,NC]

意思将所有http://domai*n/a*p*p/controller 的url重写为 http://domain/*i**ndex.php/app/controller。
L(last rule) 表明当前规则是最后一条规则,停止分析以后规则的重写。
NC(no case) 不区分大小写。
2,重写规则既可以在apache配置里写,还可以在kohana的.htaccess文件里写。
另外在项目目录下面的conf/routes.php中也可以配置url替换规则。


IIS使用ISAPI_Rewrite配置httpd.ini在URL中去掉index.php

因为国内互联网应用服务提供商提供的服务器软件很多都是IIS,此处主要对IIS下使用httpd.ini配置文件去掉index.php进行介绍,如下方法经过验证:
1、修改项目配置文件"项目路径Confconfig.php",确保URL_MODEL设置为2。
2、配置httpd.ini,配置示例如下:
[ISAPI_Rewrite]
# 3600 = 1 hour
CacheClockRate 3600
RepeatLimit 32
#如下为关键的地方,示例中Public、Rbac/Tpl/Admin/Public 下都有一些图片、CSS文件,如果不做排除,那么网页不能正常显示。如果您希望排除更多的目录,请在如下代码中增加,增加格式为(?!目录路径)。
RewriteRule /(?!Public)(?!Rbac/Tpl/Admin/Public)(.*) /index.php/$1 [L]

相关文章

精彩推荐