您尚未登录。

楼主 #1 2020-03-02 15:37:09

春风吹又生
会员
注册时间: 2020-02-25
已发帖子: 61
积分: 60

试一试 thinkphp 的 urlwrite 重写

安装thinkphp5 参考这里: https://whycan.cn/t_3445.html#p34444

安装好 thinkphp5 之后, 发现一定要用这个网址访问:
http://localhost/index.php/index/index/index

可是我记得以前可以用这个地址访问:
http://localhost/index/index/index

离线

楼主 #2 2020-03-02 15:54:55

春风吹又生
会员
注册时间: 2020-02-25
已发帖子: 61
积分: 60

Re: 试一试 thinkphp 的 urlwrite 重写

后来找到官方文档: https://www.kancloud.cn/manual/thinkphp5/177576

1. 先执行: a2enmod rewrite

2. AllowOverride None 改为 AllowOverride All

        <Directory /var/www/html>
                AllowOverride All
        </Directory>

3. 把下面的内容保存为.htaccess文件放到应用入口文件的同级目录下:

<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine on

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

现在可以了.

离线

楼主 #3 2020-03-02 16:15:42

春风吹又生
会员
注册时间: 2020-02-25
已发帖子: 61
积分: 60

Re: 试一试 thinkphp 的 urlwrite 重写

居然搜到了关于上面 .htaccess 重写脚本的解释: https://segmentfault.com/q/1010000009402187

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>

1、排除一些条件,必须两个条件都满足后才重定向到index.php
//如果你访问的文件不等于目录

RewriteCond %{REQUEST_FILENAME} !-d
//如果你访问不是文件,比如你可能访问的JPEG等图片文件

RewriteCond %{REQUEST_FILENAME} !-f

2、^(.*)$ 匹配所有的路径映射到入口文件 index.php/$1

3、标签 [QSA,PT,L] QSA:表示保留参数如get传值?xxx==xx...; PT:再把这个URL交给Apache处理;L:作为最后一条;
PT和L可加可不加。

944234436-5916a361d533b_articlex.png

离线

楼主 #4 2020-03-02 17:23:11

春风吹又生
会员
注册时间: 2020-02-25
已发帖子: 61
积分: 60

Re: 试一试 thinkphp 的 urlwrite 重写

搞定一个简单的重写:

http://localhost/t_1122.html

<===>

http://localhost/index.php/index/index/index/viewtopic/1122

<IfModule mod_rewrite.c>
  Options +FollowSymlinks -Multiviews
  RewriteEngine On

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^t_(\d+).html$ index.php/index/index/viewtopic/id/$1 [QSA,PT,L]
</IfModule>

application/index/controller/Index.php:

<?php
namespace app\index\controller;

class Index
{
    public function viewtopic($id = 100)
    {
        return 'hello: ' . $id;
    }
}

离线

楼主 #5 2020-03-02 17:30:25

春风吹又生
会员
注册时间: 2020-02-25
已发帖子: 61
积分: 60

Re: 试一试 thinkphp 的 urlwrite 重写

找到一篇关于 apache url rewrite 的配置文件:
Apache mod_rewrite Introduction

rewrite_backreferences.png

syntax_rewriterule.png

syntax_rewritecond.png

离线

#6 2020-03-02 21:26:45

mango
会员
所在地: 芒果派
注册时间: 2017-12-02
已发帖子: 779
积分: 730

Re: 试一试 thinkphp 的 urlwrite 重写

是不是用nginx重写会更简洁些?apache的看着头晕






歪朵拉开源硬件: https://widora.cn
淘宝:  https://widora.taobao.com/

离线

#7 2020-03-03 09:43:52

aozima
会员
所在地: 深圳
注册时间: 2019-05-25
已发帖子: 446
积分: 313.5
个人网站

Re: 试一试 thinkphp 的 urlwrite 重写

nginx 在二级目录架设php才是坑,apache反而好些。

最近编辑记录 aozima (2020-03-03 09:44:02)

离线

#8 2021-01-10 13:12:15

落日余晖
会员
注册时间: 2019-11-23
已发帖子: 34
积分: 26.5

Re: 试一试 thinkphp 的 urlwrite 重写

mango 说:

是不是用nginx重写会更简洁些?apache的看着头晕

我个人感觉还好, 就是一个正则表达式而已.

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn