• 技术文章 >后端开发 >php教程

    nginx - thinkphp 如何实现url的rewrite

    2016-06-06 20:51:40原创382
    thinkphp 如何实现 "http://localhost/index.php?/模块/操作&quo... 变为 "http://localhost/模块/操作"
    我想把URL改成这种形式,看起来会比较舒服,但是不知道怎么实现。
    是在conifg里面加参数,还是在nginx里面用rewrite?

    回复内容:

    thinkphp 如何实现 "http://localhost/index.php?/模块/操作&quo... 变为 "http://localhost/模块/操作"
    我想把URL改成这种形式,看起来会比较舒服,但是不知道怎么实现。
    是在conifg里面加参数,还是在nginx里面用rewrite?

    1.在配置文件将URL模式改成

    'URL_MODEL'          => '2', //REWRITE模式

    2.Apache服务器的话,将下面的内容保存为.htaccess文件放到入口文件的同级目录

    
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
    

    Nginx.conf中配置

    location / { // …..省略部分代码
       if (!-e $request_filename) {
       rewrite  ^(.*)$  /index.php?s=$1  last;
       break;
        }
     }

    参考资料:
    http://doc.thinkphp.cn/manual/url_rew...
    http://doc.thinkphp.cn/manual/hidden_...

    建议楼主把手册看一边先,毕竟这是基础的东西。

    用 rewrite 的方式。
    官方文档: 隐藏index.php

    这个需要在nginx的配置里头rewrite。

    原来的格式可以保证在服务器(apache/nginx等)没有rewrite模块的情况下,框架也能正常运行。

    rewrite可以类似于:

    location /your_app/ {
        if (!-f $request_filename) { #如果请求到css等资源则略过
            rewrite ^/your_app/(.*)$ /your_app/index.php?$1; 
        }
    }

    nginx上rewrite建议使用官方和我都推荐的try_files方法 ;)

    location / {
        try_files $uri $uri/ /index.php$uri?$args;
    }

    看看为什么使用try_files而不是用if来判断

    传送门1:http://wiki.nginx.org/HttpCoreModule#...
    传送门2:http://wiki.nginx.org/IfIsEvil

    开启路由匹配,然后再apache里开启rewrite模式

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:thinkphp php url nginx
    上一篇:PHP如何处理同重复参数名的参数 下一篇:php 字符串有长度,但内容为空?
    20期PHP线上班

    相关文章推荐

    • 【活动】充值PHP中文网VIP即送云服务器• PHP8.2将有哪些改动(性能改进、新特性)!• PHP8.2最新进展,即将进行新特性冻结!• 请问一个文本存放的有关问题,没搞明白如何整 • 请教zendframework有导入数据到数据库,导出数据库的接口吗 • 这个如何采集,有点难倒小弟我了
    1/1

    PHP中文网