thinkphp 5.1的URL重寫,偽靜態設定
阿新 • • 發佈:2019-01-07
Thinkphp 5.1 可以通過URL重寫隱藏應用的入口檔案index.php(也可以是其它的入口檔案,但URL重寫通常只能設定一個入口檔案),下面是相關伺服器的配置參考:
Apache
httpd.conf配置檔案中載入了mod_rewrite.so模組
AllowOverride None 將None改為 All
把下面的內容儲存為.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
如果你的伺服器環境支援ISAPI_Rewrite的話,可以配置httpd.ini檔案,新增下面的內容:
RewriteRule (.*)$ /index\.php\?s=$1 [I]
在IIS的高版本下面可以配置web.Config,在中間新增rewrite節點:RewriteRule (.*)$ /index\.php\?s=$1 [I]RewriteRule (.*)$ /index\.php\?s=$1 [I] <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>
Nginx
在Nginx低版本中,是不支援PATHINFO的,但是可以通過在Nginx.conf中配置轉發規則實現:
location / { // …..省略部分程式碼
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
}
}
其實內部是轉發到了ThinkPHP提供的相容URL,利用這種方式,可以解決其他不支援PATHINFO的WEB伺服器環境。
如果你的應用安裝在二級目錄,Nginx的偽靜態方法設定如下,其中youdomain是所在的目錄名稱。
http://serverName/模組/控制器/操作/[引數名/引數值...]
原來的訪問URL:
http://serverName/index.php/模組/控制器/操作/[引數名/引數值...]
設定後,我們可以採用下面的方式訪問:
http://serverName/模組/控制器/操作/[引數名/引數值...]如果你沒有修改伺服器的許可權,可以在index.php入口檔案做修改,這不是正確的做法,並且不一定成功,視伺服器而定,只是在框架執行前補全$_SERVER['PATH_INFO']引數。
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI' ];