2018年5月

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

Apache配置

站点配置:

<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框架支持最好的了!

<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

站点配置

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内的最后面即可。

<?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