Home Backend Development PHP Tutorial Specific implementation method of pseudo-static page in PHP_PHP tutorial

Specific implementation method of pseudo-static page in PHP_PHP tutorial

Jul 15, 2016 pm 01:28 PM
php one specific exist accomplish right Way method static page

大家也许对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 class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>Php2Html_FileUrl</FONT></SPAN><SPAN> = $_SERVER["REQUEST_URI"];   </SPAN></SPAN><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></SPAN><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></SPAN><LI class=alt><SPAN>foreach($Php2Html_UrlQueryStrList as $Php2Html_UrlQueryStr)   </SPAN><LI class=""><SPAN>{   </SPAN><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></SPAN><LI class=""><SPAN>$_GET[$Php2Html_TmpArray[0]] = $Php2Html_TmpArray[1];   </SPAN><LI class=alt><SPAN>}   </SPAN><LI class=""><SPAN>echo '假静态:$_GET变量</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>br</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></span></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>
Copy after login

然后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>
Copy after login

然后在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>
Copy after login
这样$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 of this Website
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

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles