用PHP实现:将动态网站整个网站静态化时用到(不光可以静态化PHP的网站,其他语言的也可以)
1、站点目录下创建index.php文件:
<?php //站点域名 Sta::srart("https://域名"); class Sta{ public static $domain = ''; public static function srart($domain){ self::$domain = $domain; self::handle(); } public static function error($msg){ echo $msg; die(); } public static function handle(){ $path = $_SERVER ['REQUEST_URI']; $path1 = explode('?',$path)[0]; $dir_arr = explode('/',$path1); $dir = []; $file_name = ''; foreach ($dir_arr as $key=>$v){ if($key>0 && $key<count($dir_arr)-1){ $dir[] = $v; } if($key===count($dir_arr)-1){ $file_name = $v; } } if($file_name==''){ $file_name = 'index.html'; } $dir = implode('/',$dir); try{ $file_content = file_get_contents(self::$domain.$path); if($dir){ if (!is_dir($dir)){ if (!mkdir(iconv("UTF-8", "GBK", $dir),0777,true)) self::error("dir $dir create error"); } } file_put_contents('./'.$dir.'/'.$file_name,$file_content); echo $file_content; die(); }catch(Exception $e){ self::error($e->getMessage()); } } }
2、设置你的nginx伪静态为:
location / { if (!-e $request_filename){ rewrite ^(.*)$ /index.php?s=$1 last; break; } }
3、设置你的nginx默认文档为:
index.html index.php
至此,大功告成。
标签:index,动态,nginx,静态,网站,PHP,php From: https://www.cnblogs.com/phpyangbo/p/18114135