PHP实现伪静态化页面的具体实现方式
大家也许对PHP实现伪静态化页面方法一:
在你的程序初始化时使用如下代码:
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">Php2Html_FileUrl</font></span><span> = $_SERVER["REQUEST_URI"]; </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">Php2Html_UrlString</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">str_replace</font></span><span>("/", "", strrchr($Php2Html_FileUrl, "/")); </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">Php2Html_UrlQueryStrList</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">explode</font></span><span>("@", $Php2Html_UrlString); </span> </li> <li class="alt"><span>foreach($Php2Html_UrlQueryStrList as $Php2Html_UrlQueryStr) </span></li> <li class=""><span>{ </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">Php2Html_TmpArray</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">explode</font></span><span>("|", $Php2Html_UrlQueryStr); </span> </li> <li class=""><span>$_GET[$Php2Html_TmpArray[0]] = $Php2Html_TmpArray[1]; </span></li> <li class="alt"><span>} </span></li> <li class=""> <span>echo '假静态:$_GET变量</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span></span></font></strong><span> </span><span class="tag"><strong><font color="#006699">/></font></strong></span><span>'; </span> </li> <li class="alt"><span>print_r($_GET); </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
然后php中调用$_GET变量就像平常一样了。
连接使用方式:
****.php/param1|1234@param2|4321
和****.php?param1=1234¶m2=4321一样。
PHP实现伪静态化页面方法二:通过URL Rewrite实现链接静态化
我们知道搜索引擎对于静态页面是非常友好的,因此很多网站通过生成静态页面等手段方便爬虫抓取自己网站的内容。但是有时候一些应用并不适合全部静态化,比如数据变化非常大的论坛/贴吧系统,这时候我们可以通过URL重写来实现链接的伪静态化,即网站对外使用静态化的链接,而内部实际上仍然使用动态页面的 URL形式。比如像这样一个链接:http://www.ci123.com/abc.php?action=a&id=1,我们可以改写成http://www.ci123.com/abc/a/1.html的形式。这是搜索引擎优化最重要的内容之一,它还有一个额外的好处,可以使页面有一个永久链接,即便以后网站系统内部链接有变化,通过适当改变Rewrite规则就可以保证原先的外部URL一直有效。
下面介绍2种简单的Apache+PHP下实现URL重写的方法,第一种适合有服务器配置权限的用户,第二种适合租用空间的用户,也作为我近期的学习心得的整理。
1、对于有服务器配置权限的用户,推荐使用Apache的mod_rewrite模块,这里假设已经安装好mod_rewrite模块。打开Apache的配置文件,找到相应主机的部分,添加以下代码:
<ol class="dp-xml"> <li class="alt"><span><span>RewriteEngine On </span></span></li> <li class=""> <span>RewriteRule ^/abc/([a-z]+)/([0-9]+).html$ /abc.php?</span><span class="attribute"><font color="#ff0000">action</font></span><span>=$1&</span><span class="attribute"><font color="#ff0000">id</font></span><span>=$2 </span> </li> </ol>
然后在shell里执行service httpd reload,让Apache重新载入配置就好了。现在在PHP页面里面我们可以把链接写成 abc/a/1.html的形式,Apache在解析这个 URL的时候会rewrite成abc.php?action=a&id=1的形式,并返回正确的页面。运用正则表达式我们可以实现几乎任何我们想要的链接形式,mod_rewrite模块的功能异常强大,这里只是一个及其简单的应用。
2、对于租用空间的用户,一般都没有办法修改Apache的配置,这里有个变通的方法,原理是这样的:当要传递参数访问PHP 页面时,正常情况下是通过自动全局变量$_GET来获得,比如上面的链接,在页面里可以通过$_GET['action'] 和 $_GET['id'] 来获得,重写URL后就不行了。现在在每个页面里require一个url_rewrite.php文件,里面代码如下:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">filename</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">basename</font></span><span>($_SERVER['SCRIPT_NAME']); </span></span></li> <li class=""><span> </span></li> <li class="alt"><span>if (strtolower($filename) == "abc.php"){ </span></li> <li class=""><span>if (!empty($_GET[id])){ </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">id</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">intval</font></span><span>($_GET[id]); </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">action</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">intval</font></span><span>($_GET[action]); </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>else { </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">nav</font></span><span> = $_SERVER["REQUEST_URI"]; </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">script</font></span><span> = $_SERVER["SCRIPT_NAME"]; </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">nav</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">ereg_replace</font></span><span>("^$script", "", urldecode($nav)); </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">vars</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">explode</font></span><span>("/", $nav); </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">action</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">intval</font></span><span>($vars[1]); </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">id</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">intval</font></span><span>($vars[2]); </span> </li> <li class="alt"><span> } </span></li> <li class=""><span>} </span></li> </ol>
需要注意的是这种PHP实现伪静态化页面方法效率较第一种低,第一种方法是在WEB服务器URL解析过程中实现的,而这里是在PHP页面解析过程里实现的,第2种方法只是变通,不得已而为之,要修改链接形式很不方便也不灵活。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
