开启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?id=$1 #这里可以设置多个重写的规则 #RewriteRule news-id.html$ error.php </Directory> </VirtualHost>
4.在相应的目录下编写.htaccess 重写规则
例子:
<IfModule rewrite_module> RewriteEngine On RewriteRule news-id(\d+).html$ show.php?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)。

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











There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

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.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

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.

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

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

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.
