Home  >  Article  >  Backend Development  >  Getting started with PHP pseudo-static based on tutorial_PHP tutorial

Getting started with PHP pseudo-static based on tutorial_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:14:19852browse

In php, we can use apache to achieve pseudo-static or php to operate, but currently I only know the pseudo-static of apache and the pseudo-static of php code.

Apache pseudo-static html (URL Rewrite) setting method

phpma1 Open Apache’s configuration file httpd.conf.

phpma2 Remove the # in front of #LoadModule rewrite_module modules/mod_rewrite


1. apache

Now we can implement pseudo-static pages, write down the following rules:

The code is as follows
 代码如下 复制代码

#liunx下需要写

rewriteengine on

rewriterule ([a-za-z]{1,})-([0-9]{1,}).html$ index.php?action=&id=

Copy code


#Need to write under liunx

rewriteengine on

rewriterule ([a-za-z]{1,})-([0-9]{1,}).html$ index.php?action=$1&id=$2

 代码如下 复制代码

RewriteEngine on  #开启重写
RewriteRule ^/$ index.php  #表示用“/”就可以访问index.php
RewriteRule ^about_(d*)/$ about/about.php?id=  #表示可以使用about_22/访问到about/about.php?id=22页面。注意$前面的“/”
RewriteRule ^about_(d*).html$ about/about.php?id=  #表示可以使用about_22.html访问到about/about.php?id=22页面RewriteRule ^news_(d*)_(d*).html$ news/news.php?id=&page=  #表示可以使用news_11_2.html访问到news/news.php?id=11&page=2页面。表示第一个参数,表示第二个参数

([a-za-z]{1,})-([0-9]{1,}).html$ is the rule, index.php?action=$1&id=$2 is the format to be replaced, $1 represents the A bracket matching value, $2 represents the second

Then restart apache

2. .htaccess Create a new file in the directory and name it .htaccess

, writing of .htaccess file


Create a file in the root directory of the website. The name is .htaccess and the writing method is:

RewriteEngine on #Turn on rewriting RewriteRule ^/$ index.php #Indicates that you can access index.php with "/"
The code is as follows

Copy code
 代码如下 复制代码

< ?php
//利用server变量 取得PATH_INFO信息 该例中为 /1,100,8630.html 也就是执行脚本名后面的部分
if(@$path_info =$_SERVER["PATH_INFO"]){
//正则匹配一下参数
if(preg_match("//(d+),(d+),(d+).html/si",$path_info,$arr_path)){
$gid =intval($arr_path[1]); //取得值 1
$sid =intval($arr_path[2]); //取得值100
$softid =intval($arr_path[3]); //取得值8630
}else die("Path:Error!");
//相当于soft.php?gid=1&sid=100&softid=8630
}else die('Path:Nothing!');
//就是这么简单了。~)
?>

RewriteRule ^about_(d*)/$ about/about.php?id=$1 #Indicates that you can use about_22/ to access the about/about.php?id=22 page. Note the "/" in front of $ RewriteRule ^about_(d*).html$ about/about.php?id=$1 #Indicates that you can use about_22.html to access the about/about.php?id=22 page RewriteRule ^news_(d*)_(d*) .html$ news/news.php?id=$1&page=$2 #Indicates that you can use news_11_2.html to access the news/news.php?id=11&page=2 page. $1 represents the first parameter, $2 represents the second parameter

As can be seen from the above, if we have the following link About us Then the page we access is the same as the page accessed using the following About us
2.php code For example: http://www.xxxx.com/soft.php/1,100,8630.html
The code is as follows Copy code
< ?php<🎜> //Use the server variable to obtain the PATH_INFO information. In this example, it is /1,100,8630.html, which is the part after the execution script name <🎜> if(@$path_info =$_SERVER["PATH_INFO"]){<🎜> //Regular match parameters <🎜> if(preg_match("//(d+),(d+),(d+).html/si",$path_info,$arr_path)){<🎜> $gid =intval($arr_path[1]); //Get value 1<🎜> $sid =intval($arr_path[2]); //Get value 100<🎜> $softid =intval($arr_path[3]); //Get value 8630<🎜> }else die("Path:Error!");<🎜> //Equivalent to soft.php?gid=1&sid=100&softid=8630<🎜> }else die('Path:Nothing!');<🎜> //It's that simple. ~)<🎜> ?> http://www.bkjia.com/PHPjc/629001.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629001.htmlTechArticleIn php we can use apache to achieve pseudo-static and also use php to operate, but currently I only know apache Pseudo-static and pseudo-static of PHP code. Apache pseudo-static html (URL Rewri...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn