1. 程式人生 > >thinkphp5 Apache / IIs環境下 URL重寫

thinkphp5 Apache / IIs環境下 URL重寫

URL重寫

好像thinkPHP5新版本 隱藏index.php隱藏index.php 都寫好了
public 隱藏
獨立主機可以直接把根目錄指向public下
虛擬主機可以把public下的index.php放到根目錄

public下原有index.php
// 定義應用目錄
define(‘APP_PATH‘, __DIR__ . ‘/../application/‘);
// 加載框架引導文件
require __DIR__ . ‘/../thinkphp/start.php‘;
放到根目錄下的index.php
// 定義應用目錄
define(‘APP_PATH‘, __DIR__ . ‘/application/‘);
// 加載框架引導文件
require __DIR__ . ‘/thinkphp/start.php‘;

Apache下隱藏index.php
tp5好像都給寫好了.htaccess文件 index.php都給隱藏掉了
如果是index.php移到根目錄了只需要把.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>

iis下隱藏
有的主機後臺有URL Rewrite直接寫規則就可以了

沒有直接在根目錄創建web.config

其中代碼
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="WPurls" enabled="true" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php/{R:0}" />
                </rule>
            </rules>
        </rewrite>

        <defaultDocument>
            <files>
                <clear />
                <add value="index.php" />
                <add value="index.html" />
            </files>
        </defaultDocument>
    </system.webServer>
</configuration>

thinkphp5 Apache / IIs環境下 URL重寫