小蚂蚁学习页面静态化(完结)——伪静态的实现和配置方法

伪静态的做用:1.让url更加美观。2.对搜索引擎更加友好。php

php处理伪静态案例分析(path_info模式)html

备注:nginx服务器默认下是不支持path_info模式的,须要去配置nginx

经过正则表达式去分析伪静态url的特性,很简单,直接上代码web

<?php 
	//匹配这个url地址 /index.php/2/1.html
	if(preg_match('/(\d+)\/(\d+)\.html/', $_SERVER['PAHT_INFO'],$arr)){
		$type	=	$arr[1];
		$category_id	=	$arr[2];
		
		//一些业务逻辑处理	好比查询数据库
	}else{
		//一些业务逻辑处理
	}

 ?>

Apache下配置rewrite方法正则表达式

    1.虚拟域名的配置 数据库

    2.httpd_vhost.conf设置
apache

虚拟域名的配置windows

    1. httpd.conf 文件开启相关模式
服务器

        去掉一下两句以前的 #  
性能

            LoadModule rewrite_module modules/mod_rewrite.so

            Include conf/extra/httpd-vhosts.conf

    2. 打开/extra/httpd_vhost.conf文件,在里面配置相关域名和伪静态规则

            <VirtualHost *:80>(这里能够指定ip)

                ServerAdmin webmaster@dummy-host2.example.com

                DocumentRoot "D:\ai\Program Files\phpStudy\WWW\demo"(这里指定路径)

                ServerName www.demo.com    (这里指定域名)

                ErrorLog "logs/dummy-host2.example.com-error.log"

                CustomLog "logs/dummy-host2.example.com-access.log" common


                RewriteEngine on    (开启伪静态引擎)

                RewriteCond    %{DOCUMENT_ROOT}%{REQUEST_FILENAME}!-d    

                RewriteCond    %{DOCUMENT_ROOT}%{REQUEST_FILENAME}!-f

                (上面的这两句是说,若是访问的url路径有真实存在的静态页面,就加载该静态页面,若是不存在就执行伪静态。固然,若是但愿无论有没有页面,都要执行伪静态的话,就把上面两句删了就好)

                RewriteCond    ^/detail/([0-9]+)$ /detail.php?id=$1    (配置伪静态路径)

            </VirtualHost>

    3. 修改    c:windows/system32/drivers/etc/hosts

            添加 127.0.0.1 www.demo.com

这样apache下rewrite配置,就妥妥搞定了。

nginx下rewrite配置方法

找到配置文件 cd /etc/nginx/conf.d

找到对应页面的配置文件,编辑它

    ……

        if(!-$request_filename){

            rewrite ^/detail/(\d+).html$ /detail.php?id=$1 last;

            break;

        }

    ……

这样就能够了。by the way,伪静态配置的太多,也是会影响服务器的性能的。

相关文章
相关标签/搜索