search
HomeBackend DevelopmentPHP TutorialSpecific implementation method of pseudo-static page in PHP_PHP tutorial

大家也许对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>
这样$action和$id也得到了,页面里链接可以写成abc.php/a/1的形式来访问相应页面。

需要注意的是这种PHP实现伪静态化页面方法效率较第一种低,第一种方法是在WEB服务器URL解析过程中实现的,而这里是在PHP页面解析过程里实现的,第2种方法只是变通,不得已而为之,要修改链接形式很不方便也不灵活。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446426.htmlTechArticle大家也许对 PHP实现伪静态化页面方法一: 在你的程序初始化时使用如下代码: ? php $ Php2Html_FileUrl =$_SERVER["REQUEST_URI"]; $ Php2Html_UrlString = s...
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
PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

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 vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

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: The Foundation of Many WebsitesPHP: The Foundation of Many WebsitesApr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

Beyond the Hype: Assessing PHP's Role TodayBeyond the Hype: Assessing PHP's Role TodayApr 12, 2025 am 12:17 AM

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

What are Weak References in PHP and when are they useful?What are Weak References in PHP and when are they useful?Apr 12, 2025 am 12:13 AM

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

Explain the __invoke magic method in PHP.Explain the __invoke magic method in PHP.Apr 12, 2025 am 12:07 AM

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment