前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >服务器配置URL重写隐藏thinkphp5入口文件

服务器配置URL重写隐藏thinkphp5入口文件

作者头像
jwj
发布2022-05-18 11:27:18
7580
发布2022-05-18 11:27:18
举报
文章被收录于专栏:用户1069690的专栏

如果没配置URL重写规则的情况下,每次访问都要加上index.php,看起来是没这么美观和方便的。 例如:http://test.ll00.cn/index.php/index/test/hello 本文主要记录Apache、Nginx和IIS配置。

Apache配置

站点配置:

代码语言:javascript
复制
<VirtualHost *:80>
    # 将PHP可以访问的文件限制到指定的目录树(http://php.net/manual/zh/ini.core.php#ini.open-basedir)
    # 配置这个主要是有些环境默认把open_basedir设置为DocumentRoot,导致thinkphp无法正常使用
    # 不是所有环境都这样,所有看情况配置
    php_admin_value open_basedir "/home/wwwroot/www.ll00.cn:/tmp/:/var/tmp/:/proc/"
    DocumentRoot "/home/wwwroot/www.ll00.cn/public"
    ServerName www.ll00.cn
    <Directory "/home/wwwroot/www.ll00.cn">
        SetOutputFilter DEFLATE
        Options FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
        DirectoryIndex index.html index.php
    </Directory>
</VirtualHost>

.htaccess配置:

需要Apache加载mod_rewrite.so模块,并且将AllowOverride配置项设置为All,例如AllowOverride All 框架安装后,默认提供一个.htaccess文件,里面就写好了重写规则,一般都是安装即用。 可以说,Apache是对thinkphp框架支持最好的了!

代码语言:javascript
复制
<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>

Nginx

站点配置

代码语言:javascript
复制
server
{
    listen 80;
    server_name www.ll00.cn;
    index index.php index.html index.htm default.php default.htm default.html;
    # 站点目录
    root /www/wwwroot/www.ll00.cn/public;
    
    # URL重写规则
    location / {
        if (!-e $request_filename){
            rewrite  ^(.*)$  /index.php?s=$1  last;   break;
        }
    }
}

IIS

IIS这个了解不大多,只是简单的尝试了下,后面坑太多,放弃了。

web.Config文件配置:

在IIS的高版本下面可以配置下面的URL重写规则,如果不行,再试试其它方式。 主要是rewrite节点的配置,如果配置文件已有其它配置,将rewrite节点的配置放在system.webServer内的最后面即可。

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="OrgPage" stopProcessing="true">
                    <match url="^(.*)$" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{HTTP_HOST}" pattern="^(.*)$" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

https://www.iis.net/downloads/microsoft/url-rewrite

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-05-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Apache配置
    • 站点配置:
      • .htaccess配置:
      • Nginx
        • 站点配置
        • IIS
          • web.Config文件配置:
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档