search
HomeBackend DevelopmentPHP Tutorial开启PHP的伪静态模式_php实例

一直在做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文件中,配置相应的选项.详细讲解

<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>

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

   例子:

<IfModule rewrite_module>
RewriteEngine On
RewriteRule news-id(\d+).html$ show.php&#63;id=$1
#这里可以设置多个重写的规则
#RewriteRule news-id.html$ error.php
</IfModule>

如果在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
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

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 and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

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

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

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.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor