Home Backend Development PHP Tutorial 开启PHP的伪静态模式_php实例

开启PHP的伪静态模式_php实例

Jun 07, 2016 pm 05:10 PM

一直在做php的开发工作.在开发的过程中老早就听说了“伪静态”这一说。但是一直没有对其进行了解。今天通过本篇文章仔细学习下。

首先,什么是伪静态:

伪静态又名URL重写,是动态的网址看起来像静态的网址。换句话说就是,动态网页通过重写 URL 方法实现去掉动态网页的参数,但在实际的网页目录中并没有必要实现存在重写的页面。

1.检测Apache是否支持mod_rewrite

通过php提供的phpinfo()函数查看环境配置,通过Ctrl+F查找到“Loaded Modules”,其中列出了所有
apache2handler已经开启的模块,如果里面包括“mod_rewrite”,则已经支持,不再需要继续设置。

如果没有开启“mod_rewrite”,则打开目录 您的apache安装目录“/apache/conf/” 下的 httpd.conf 文
件,通过Ctrl+F查找到“LoadModule rewrite_module”,将前面的”#”号删除即可。

如果没有查找到,则到“LoadModule” 区域,在最后一行加入“LoadModule rewrite_module modules/mod_rewrite.so”(必选独占一行),然后重启apache服务器即可。

2.在httpd.conf中配置虚拟主机

# Virtual hosts 启用虚拟主机
Include conf/extra/httpd-vhosts.conf

3.httpd_vhosts.conf文件中,配置相应的选项.详细讲解

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<VirtualHost *:80>

  DocumentRoot "C:/myenv/apache/htdocs/static3"

  ServerName www.hsp.com

  <Directory "C:/myenv/apache/htdocs/static3">

#Deny from All 403错误提示

Allow from All

#如果文件目录在apache目录外面,注释掉optinos 则,不能列表.

options +Indexes

#下面这个表示可以去读取 .htaccess文件,也可以直接在虚拟主机中配置.

Allowoverride All

RewriteEngine On

RewriteRule news-id(\d+).html$ error.php&#63;id=$1

#这里可以设置多个重写的规则

#RewriteRule news-id.html$ error.php

  </Directory>

 </VirtualHost>

Copy after login

4.在相应的目录下编写.htaccess 重写规则

例子:

1

2

3

4

5

6

<IfModule rewrite_module>

RewriteEngine On

RewriteRule news-id(\d+).html$ show.php&#63;id=$1

#这里可以设置多个重写的规则

#RewriteRule news-id.html$ error.php

</IfModule>

Copy after login

如果在linux下可以直接创建,

如果是在windows平台下,用记事本创建一个文件,比如abc.txt,然后另存为 .htaccess文件即可

5.重写规则,也可以直接在配置虚拟主机的段配置.

动态、静态、伪静态之间的利与弊(新)

动态网址

首先,动态网址目前对于Google来说,“不能被抓取”的说法是错误的,Google能够很好的处理动态网址并顺利抓取;其次“参数不能超过3个”的说法也不正确,Google能够抓取参数超过3个的动态网址,不过,为了避免URL太长应尽量减少参数。

其次,动态网址有其一定的优势,以上面所说的 news.php?lang=cn&class=1&id=2 为例,网址中的参数准确的告诉Google,此URL内容语言为cn、隶属于分类1、内容ID为2,更便于Google对内容的识别处理。

最后,动态网址应尽量精简,特别是会话标识(sid)和查询(query)参数,容易造成大量相同页面。

静态网址

首先,静态网址具有的绝对优势是其明晰,/product/nokia/n95.html和/about.html可以很容易被理解,从而在搜索结果中可能点击量相对较高。

其次,静态网址未必就是最好的网址形式,上述动态网址中说到,动态网址能够告诉Google一些可以识别的参数,而静态网址如果文档布置不够恰当(如:过于扁平化,将HTML文档全放在根目录下)及其他因素,反而不如静态网址为Google提供的参考信息丰富。

最后,樂思蜀觉得Google此文中是否有其隐藏含义?“更新此种类型网址的页面会比较耗费时间,尤其是当信息量增长很快时,因为每一个单独的页面都必须更改编译代码。”虽然所说的是网站,但在Google系统中是否同样存在这样的问题呢?

伪静态网址

首先,伪静态网址不能让动态网址“静态化”,伪静态仅仅是对动态网址的一个重写,Google不会认为伪静态就是HTML文档。

其次,伪静态可取,但应把重心放在去除冗余参数、规范URL、尽可能的避免重复页上。

最后,伪静态有很大潜大危险,最好在对网站系统、网站结构、内容分布、参数意义熟悉的情况下使用。

在写伪静态规则时,应保留有价值的参数,不要将有价值的参数全部精简掉,如前面例子中的 news.php?lang=cn&class=1&id=2 最好重写为 news-cn-class1-id2.html,而不是过份精简重写为 news-2.html。

再就是伪静态中一定不能包含会话标识(sid)和查询(query)参数,/product.asp?sid=98971298178906&id=1234 这样的动态网址,其中的sid本来Google能够识别并屏蔽,但如果重写为 /product/98971298178906/1234,Google不但无法识别,还在整站中造成无限重复页面(每个会话都会产生一个新的会话ID)。

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks 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)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

PHP Logging: Best Practices for PHP Log Analysis PHP Logging: Best Practices for PHP Log Analysis Mar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles