• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

url优化|隐藏index.php

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

隐藏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 入口文件的配置就成功了。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php环境搭建发布时间:2022-07-10
下一篇:
php实现二分查找发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap