隐藏index.php

 

一、codeigniter

codeigniter和许多php框架一样,有个单一入口index.php,从url上看,显得很不友好。通过apache的rewirte,是可以隐藏掉的,实现伪url。

打开codeigniter下system\application\config中的config.php

找到$config['index_page'] = "index.php"; 改为 $config['index_page'] = "";

修改apache conf下的配置文件 httpd.conf

# LoadModule rewrite_module modules/mod_rewrite.so

去掉上面的#。(开启改功能)

找到

<Directory "D:/phpspace">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important. Please see
    # http://httpd.apache.org/docs/2.2/mod/core.html#options 
    # for more information.
    #
    Options Indexes FollowSymLinks

#
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride None

#
    # Controls who can get stuff from this server.
    #
    Order allow,deny
    Allow from all

</Directory>

将AllowOverride None改为 AllowOverride All

在工程的同级目录下,建立.htaccess文件 内容如下

RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php|images|robots\.txt)
    RewriteRule ^(.*)$ /seam/index.php/$1 [L]

连接中既可以不比有index.php

二、ThinkPHP

去掉 URL 中的 index.php

ThinkPHP 作为 PHP 框架,是单一入口的,那么其原始的 URL 便不是那么友好。但 ThinkPHP 提供了各种机制来定制需要的 URL 格式,配合 Apache .htaccess 文件,更是可以定制出人性化的更利于 SEO 的 URL 地址来。

.htaccess 文件是 Apache 服务器中的一个配置文件,它负责相关目录下的网页配置。我们可以利用 .htaccess 文件的 Rewrite 规则来隐藏掉 ThinkPHP URL 中的 index.php 文件(即入口文件),这也是 ThinkPHP URL 伪静态的第一步。

例如原来的 URL 为:

http://127.0.0.1/index.php/Index/insert 

去掉 index.php 之后变为:

http://127.0.0.1/Index/insert 

如此一来,就变成了 http://服务器地址/应用模块名称/操作名称[/变量参数] 的常见 URL 格式。

更改 Apache httpd.conf 配置文件

提示:如果在虚拟主机商配置,请直接配置第三、四步,因为支持 .htaccess 的空间已经配置好了前面两步。

用编辑器打开 Apache 配置文件 httpd.conf(该文件位于 Apache 安装目录Apache2conf),并按如下步骤修改,。

一、加载了 mod_rewrite.so

确认加载了 mod_rewrite.so 模块(将如下配置前的 # 号去掉):

LoadModule rewrite_module modules/mod_rewrite.so 

二、更改 AllowOverride 配置

更改需要读取 .htaccess 文件的目录,将原来的目录注释掉:

#<Directory "C:/Program Files/Apache Group/Apache2/htdocs"> 
<Directory E:/html/myapp> 

更改 AllowOverride None 为 AllowOverride FileInfo Options ,更改后的配置如下所示:

#<Directory "C:/Program Files/Apache Group/Apache2/htdocs"> 
<Directory E:/html/myapp>     
AllowOverride FileInfo Options 
</Directory> 

.htaccess 是基于目录来控制的,<Directory E:/html/myapp> 该句即表示需要读取 .htaccess 文件的目录,要根据实际具体 Apache 的解析目录来配置。虚拟主机如果提供 .htaccess 控制,一般都已经配置好了。

三、添加 .htaccess 文件 Rewrite 规则

在需要隐藏 index.php 的目录下(本教程中为 E:/html/myapp,也即入口文件所在目录)创建 .htaccess 文件,并写入如下规则代码:

<IfModule mod_rewrite.c> 
RewriteEngine on  
#不显示index.php  
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]  </IfModule> 

四、更改项目配置文件

编辑项目配置文件 Conf/config.php ,将 URL 模式配置为 2(Rewrite模式):

    'URL_MODEL'=>2, 

至此,各个配置已经完成。保存各配置文件后,重启 Apache 服务器并删除 Runtime 目录下的项目缓存文件,在浏览器访问隐藏 index.php 后的地址测试是否成功:

http://127.0.0.1/html/myapp/Index/index 

如果访问成功,那么利用 Apache .htaccess 文件的 Rewrite 规则隐藏 index.php 入口文件的配置就成功了。

url优化|隐藏index.php的更多相关文章

  1. apache 配置rewrite模块,URL中隐藏index.php

    打开httpd.conf 去掉下面的井号 #LoadModule rewrite_module modules/mod_rewrite.so把前面的警号去掉 在网站根目录添加.htaccess Rew ...

  2. ThinkPHP CI codeignitor 框架 apache 重写 url 隐藏index.php 服务器 报错:Object not found! 可能是.htaccess隐藏index.php

    隐藏index.php可以去掉URL地址里面的入口文件index.php,但是需要额外配置WEB服务器的重写规则.以Apache为例,需要在入口文件的同级添加.htaccess文件(官方默认自带了该文 ...

  3. yii框架 隐藏index.php 以及美化URL(pathinfo模式访问)

    首先我们分步骤来: 安装好 yii 以后  我们看到的url地址如下所示: http://www.3w.com/MyApp/backend/web/index.php?r=site%2Flogin 我 ...

  4. ThinkPHP 隐藏URL中的 index.php

    去掉 URL 中的 index.php 通常的URL里面含有index.php,为了达到更好的SEO效果可能需要去掉URL里面的index.php ,通过URL重写的方式可以达到这种效果,通常需要服务 ...

  5. Yii 1.1 URL两个笔记 同时支持PATH于GET路由和隐藏index.php

    同时支持PATH于GET格式路由(修改框架文件 简直坑) framework/web/CUrlManager.php parseUrl方法 第一行判断修改成 if($this->getUrlFo ...

  6. ThinkPHP路由去掉隐藏URL中的index.php

    官方默认的.htaccess文件 <IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews RewriteEngine On ...

  7. ThinkPHP的URL优化

    在刚刚安装好tp框架时,我们的路径一般是这样的: http://localhost/index.php/Home/Index/index?name=cly 我们可以通过下面一步步的步骤来优化这个路径 ...

  8. 网站url路径优化方法完全讲解 (url优化、基于tp5、API接口开发)

    url优化可是网站开发的必备高阶技能,先看本实例优化前后效果比较: (同为调用前台模块下的index控制器下的index方法) 优化前:www.tp5.com/tp5/public/index.php ...

  9. ThinkPHP5 隐藏index.php问题

    隐藏index.php 可以去掉URL地址里面的入口文件index.php,但是需要额外配置WEB服务器的重写规则. 以Apache为例,需要在入口文件的同级添加.htaccess文件(官方默认自带了 ...

随机推荐

  1. 《3+1团队》【Alpha】Scrum meeting 1

    项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 3+1团队 团队博客地址 https://home.cnblogs.com/u/3-1group ...

  2. git 超时 时长 设置?

    [Pipeline] { (Checkout) [Pipeline] checkout > git.exe rev-parse --is-inside-work-tree # timeout=1 ...

  3. python interview questions

    referce:python interview questions top 50 refercence:python interview questions top 15 summary Q: wh ...

  4. scrapy example

    scrapy example scrapy with pycharm import win32api 出现ImportError: DLL load failed 错误的解决方法 pip instal ...

  5. HTML中 DOM操作的Document 对象详解(收藏)

    Document 对象Document 对象代表整个HTML 文档,可用来访问页面中的所有元素.Document 对象是 Window 对象的一个部分,可通过 window.document 属性来访 ...

  6. [LUOGU] P3205 [HNOI2010]CHORUS 合唱队

    为了在即将到来的晚会上有更好的演出效果,作为AAA合唱队负责人的小A需要将合唱队的人根据他们的身高排出一个队形.假定合唱队一共N个人,第i个人的身高为Hi米(1000<=Hi<=2000) ...

  7. POJ 1383 Labyrinth (树的直径求两点间最大距离)

    Description The northern part of the Pyramid contains a very large and complicated labyrinth. The la ...

  8. 算法导论 第六章 堆排序(python)

    6.1堆 卫星数据:一个带排序的的数通常是有一个称为记录的数据集组成的,每一个记录有一个关键字key,记录的其他数据称为卫星数据. 原地排序:在排序输入数组时,只有常数个元素被存放到数组以外的空间中去 ...

  9. 前端开发利器webStorm 3.0配置使用

     安装了phpstorm之后,想配置svn,结果在file->settings->Version Contorl->subversion->with conmand line ...

  10. resultType返回的是集合中的元素类型

    https://www.cnblogs.com/start-fxw/p/5900087.html