Using pseudo-static in php_PHP tutorial
上次简单的说了下php中正则表达式的使用,这一次正则表达式可以派上用场了,学习伪静态需要能够很好的使用正则表达式,那么伪静态和真静态的区别是什么呢,我觉得应该是伪静态可以节约磁盘空间、利于SEO、访问速度上没有真静态那么快。伪静态也是对apache的rewrite机制的使用,下来就来分享下吧
1.使用伪静态首先要确认打开rewrite模块
首先打开httpd.conf,找到LoadModule rewrite_module modules/mod_rewrite.so去掉前面的#即可之后重启apache,使用phpinfo确认重写模块成功启用
看到有红色这个就说明rewrite已经启用成功了喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGJyPgo8L3A+CjxwPjIuyrnTw86xvrLMrNKqz8jU2kRpcmVjdG9yecDvvNPSu77kQWxsb3dPdmVycmlkZSBBbGw8L3A+CjxwPtXi0ru+5L/J0tS809TaYXBhY2hltcRodGRvY3O1xERpcmVjdG9yeb3atePA77vy1d/Q6cTi1ve7+rXERGlyZWN0b3J5vdq148DvPC9wPgo8cD48cHJlIGNsYXNzPQ=="brush:java;">
3.apache指定首页面、错误页
首先新建一个.htaccess文件,一般是先新建一个xx.txt文件另存为即可,这个文件我就放到项目的根目录,这个文件的内容如下
DirectoryIndex index.php
ErrorDocument 404 /static2/404.php
下面先来测试404,我们先访问一个不存在的php看看404生效没有,这个是我的错误页面
<?php echo "错误页面"; ?>
下面是运行截图
首页的html如下
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>系统首页</title> </head> <body> 欢迎 </body> </html>
我们直接把地址定位到根目录,回车后就能看到我们的这个首页了
还有这么一种情况需要考虑那就是访问的时候apache列出目录结构的问题,其实很简单就在.htaccess加一句Options None,需要注意的是Directory里就不能配置Options了,否则会出现403错误
4.伪静态的使用
http://localhost/static2/view-sports-id5.html类似这种url我们应该见过很多了,这种就是一种伪静态的url了,我们看上去访问的是一个静态的html但其实不是,类似这种url像sports和id后面的5可能就是程序中要使用的参数,我们访问的其实是一个动态页面。这样的话比较利于SEO,下面上一段配置给大家看看
<IfModule rewrite_module> RewriteEngine on RewriteRule view-([a-zA-Z_]+)-id(Using pseudo-static in php_PHP tutoriald+)Using pseudo-static in php_PHP tutorial.html news.php?type=$1&id=$2 </IfModule>
RewriteEngine on的意思是启用apache的rewrite引擎
RewriteRule表示重写规则,第一个空格后面的是正则规范后面的news.php?type=$1&id=$2才是真正访问的php页面,$1表示前面正则规范的第一个子表达式的值,$2以此类推,这样我们就可以在news.php取得参数的值
同样的如果是控制器也可以在相应文件夹里写一个.htaccess,之后加上我们的重写规则
5.使用.htaccess来控制访问权限
日常的开发中我们可能在项目里面写了DAO,控制器,工具类这一大堆的php,而这些文件我们是不希望别人通过浏览器访问到,这种情况使用session来限制似乎也不奏效,这种情况使用重写规则就很简单了
RewriteRule [a-zA-Z0-9_]+Using pseudo-static in php_PHP tutorial.classUsing pseudo-static in php_PHP tutorial.php 403.html
这样写一句程序之外访问就跳转到另外一个页面,实现了访问的控制
6.RewriteCond的使用
有时我们需要判断在某种情况下才使用重写,这种情况就要使用RewriteCond了,例如我们可以判断请求的是不是一个文件(或不存在的文件),如果满足条件才执行重写规则
#如果请求的不是一个文件
RewriteCond %{REQUEST_FILENAME} !-f
#并且不是一个目录
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ccc.html index.php
这段配置的意思就是如果请求的ccc.html如果不存在则跳转到index,php
再来看最后一段配置
<ifModule rewrite_module> RewriteEngine On #你怎么知道,这个请求就是www.hsp.com发来的. referer #如果你请求的是一个jpg图片, 就禁止 RewriteCond %{HTTP_REFERER} !www.hsp.com RewriteRule .*Using pseudo-static in php_PHP tutorial.jpg -[F] </ifModule>
Finally, to summarize, not all pages in daily development need to be static. For example, pages or websites with high real-time requirements such as back-end systems, fund stocks, real-time phone bill or traffic query pages, and academic qualification query pages are not suitable for static , the corresponding content is relatively stable, such as the homepage of promotional websites, you can consider using true static. If you don’t want to use true static but want to benefit SEO, pseudo-static should be a good choice.

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











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,

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

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

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.

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.

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 is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.
