PHP伪静态的几种方法
第一种 通过php程序本身伪静态
访问http://www.domain.com/rewrite.php/2/1.htmL页面打印 $_SERVER全局变量 的结果
Array
(
[HTTP_HOST] => www.duanjuzi.com
[HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 5.1; rv:52.0) Gecko/20100101 Firefox/52.0
[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
[HTTP_ACCEPT_LANGUAGE] => zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
[HTTP_ACCEPT_ENCODING] => gzip, deflate
[HTTP_CONNECTION] => keep-alive
[HTTP_UPGRADE_INSECURE_REQUESTS] => 1
[PATH] => C:\Documents and Settings\All Users\Application Data\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem
[SystemRoot] => C:\WINDOWS
[COMSPEC] => C:\WINDOWS\system32\cmd.exe
[PATHEXT] => .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
[WINDIR] => C:\WINDOWS
[SERVER_SIGNATURE] =>
[SERVER_SOFTWARE] => Apache/2.4.23 (Win32) OpenSSL/1.0.2j PHP/5.4.45
[SERVER_NAME] => www.duanjuzi.com
[SERVER_ADDR] => 127.0.0.1
[SERVER_PORT] => 80
[REMOTE_ADDR] => 127.0.0.1
[DOCUMENT_ROOT] => F:/WWW
[REQUEST_SCHEME] => http
[CONTEXT_PREFIX] =>
[CONTEXT_DOCUMENT_ROOT] => F:/WWW
[SERVER_ADMIN] => admin@phpStudy.net
[SCRIPT_FILENAME] => F:/WWW/rewrite.php
[REMOTE_PORT] => 4769
[GATEWAY_INTERFACE] => CGI/1.1
[SERVER_PROTOCOL] => HTTP/1.1
[REQUEST_METHOD] => GET
[QUERY_STRING] =>
[REQUEST_URI] => /rewrite.php/2/1.htmL
[SCRIPT_NAME] => /rewrite.php
[PATH_INFO] => /2/1.htmL
[PATH_TRANSLATED] => F:\WWW\2\1.htmL
[PHP_SELF] => /rewrite.php/2/1.htmL
[REQUEST_TIME_FLOAT] => 1517382871.093
[REQUEST_TIME] => 1517382871
)
rewrite.php代码如下
<?php if(@$path_info=$_SERVER["PATH_INFO"]){ preg_match('/^\/(\d+)\/(\d+).html/',$_SERVER['PATH_INFO'],$arr); echo "<pre>"; print_r($arr); } else{ echo "fail"; } |
页面访问/rewrite.php/2/1.html输出内容如下
Array ( [0] => /2/1.html [1] => 2 [2] => 1 ) |
对匹配结果数组$arr
<?php if(@$path_info=$_SERVER["PATH_INFO"]){ if(preg_match('/^\/(\d+)\/(\d+).html/',$_SERVER['PATH_INFO'],$arr){ $type=$arr[1]; $cid=$arr[2]; //通过$type $cid变量即可从数据库中获取数据 } else{ echo "fail"; } |
和访问 /rewrite.php?type=2&cid=1 的效果相同
第二种通过Apache服务器配置伪静态
一、配置虚拟主机需要3个文件
1、Apache/conf/httpd.conf
2、Apache/conf/extra/httpd-vhosts.conf
(这个地版本的apache可能没有,可自己创建,也可以不要此文件,而将配置直接写在httpd.conf里面,写在extra/httpd-vhosts.conf只是为了管理方便,不让httpd.conf文件内容很多而已)
3、C:\WINDOWS\system32\drivers\etc\hosts
二、配置步骤
① httpd.conf 找到VirtualHost example,在后面引入httpd-vhosts.conf文件:
Include conf/extra/httpd-vhosts.conf 这句话,高版本的apache里面已经写有了,只需吧Include前面的#号去掉就OK
② httpd-vhosts.conf配置文件的内容如下:
# # DocumentRoot 是网站文件存放的根目录 # ServerName 是网站域名, 需要跟 DNS 指向的域名一致 # <VirtualHost *:80> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "F:\WWW" ServerName www.duanjuzi.com ServerAlias www.dummy-host.example.com # ErrorLog "logs/dummy-host.example.com-error.log" # CustomLog "logs/dummy-host.example.com-access.log" common RewriteEngine on //重写URL规则 RewriteRule ^/test/([0-9]+).html$ /test.php?id=$1 </VirtualHost> |
。。。。。。可以写多个虚拟主机。。。。。。。
③在hosts文件里面增加一句:
127.0.0.1 www.duanjuzi.com
现在访问www.duanjuzi.com就可以访问到F:\WWW这个网站的内容了
现在访问http://www.duanjuzi.com/test/1.html 等同于访问 http://www.duanjuzi.com/test.php?id=1
如果存在同名静态目录文件,会依旧访问动态文件,这时候需要做点设置:
<VirtualHost *:80> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "F:\WWW" ServerName www.duanjuzi.com ServerAlias www.dummy-host.example.com # ErrorLog "logs/dummy-host.example.com-error.log" # CustomLog "logs/dummy-host.example.com-access.log" common RewriteEngine on RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d RewriteRule ^/test/([0-9]+).html$ /test.php?id=$1 </VirtualHost> |