htaccess伪静态规则
.htaccess文件提供了针对目录改变配置的方法, 是Apache服务器中的一个配置文件,它负责相关目录下的网页配置。通过htaccess文件,可以帮我们实现:网页301重定向、自定义404错误页面、改变文件扩展名、允许/阻止特定的用户或者目录的访问、禁止目录列表、配置默认文档等功能。
伪静态实际上是利用PHP把当前地址解析成另外一种方法进行访问网站!要学伪静态规则的写法,你必须得懂一点正则。
一、正则表达式教程
简单罗列如下:
. 换行符以外的所有字符 \w 匹配字母或数字或下划线或汉字 \s 匹配任意的空白符 \d 匹配数字 \b 匹配单词的开始或结束 ^ 匹配字符串的开始 $ 匹配字符串的结束 * 重复零次或更多次 + 重复一次或更多次 ? 重复零次或一次 {n} 重复n次 {n,}重复n次或更多次 {n,m} 重复n到m次
二、常见的.htaccess应用举例
1 防止盗链,如果来得要访问jpe jpg bmp png结尾的url 用户不是来自我们的网站,那么让他看一张我们网站的展示图片。
RewriteEngine On RewriteCond %{HTTP_REFERER} !^http://(.+.)?mysite.com/ [NC]RewriteCond %{HTTP_REFERER} !^$RewriteRule .*.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]
2 网站升级的时候,只有特定IP才能访问,其他的用户将看到一个升级页面
RewriteEngine on RewriteCond %{REQUEST_URI} !/upgrade.html$RewriteCond %{REMOTE_HOST} !^24\.121\.202\.30 RewriteRule $ http://www.linuxidc.com/upgrade.html [R=302,L]
3 把老的域名转向新域名
# redirect from old domain to new domain RewriteEngine On RewriteRule ^(.*)$http://www.yourdomain.com/$1[R=301,L]
三、常用示例
比如:http://www.yzzmf.com/index.html -> http://www.yzzmf.com/index.php
RewriteEngine On RewriteRule index.html index.php
比如:http://www.yzzmf.com/test8.html -> http://www.yzzmf.com/test.php?id=8
RewriteRule ^test([0-9]*).html$ test.php?id=$1
比如:http://www.yzzmf.com/cat-1-3.html -> http://www.yzzmf.com/cat.php?id1=1&id2=3
RewriteRule ^cat-([0-9]+)-([0-9]+)\.html$ cat.php?id1=$1&id2=$2
比如:http://www.yzzmf.com/cat-zbc2ac-3-5.html -> http://www.yzzmf.com/cat.php?id0=zbc2ac&id1=3&id2=5
RewriteRule ^cat-([a-zA-Z0-9\-]*)-([0-9]+)-([0-9]+)\.html$ cat.php?id0=$1&id1=$2&id2=$3
比如:http://www.yzzmf.com/cat1-4-3-8.html -> http://www.yzzmf.com/cat1.php?id1=4&id2=3&id3=8
RewriteRule ^cat1-([0-9]+)-([0-9]+)-([0-9]+)\.html$ cat1.php?id1=$1&id2=$2&id3=$3
比如:http://www.yzzmf.com/cat5/ -> http://www.yzzmf.com/cat.php?id1=5
RewriteRule ^cat([0-9]*)/$ cat.php?id1=$1
比如:http://www.yzzmf.com/catm6/3/ -> http://www.yzzmf.com/catm.php?id1=6&id2=3
RewriteRule ^catm([0-9]*)/([0-9]*)/$ catm.php?id1=$1&id2=$2
希望对大家有所帮助!
推荐教程:apache教程
The above is the detailed content of htaccess pseudo-static rules. For more information, please follow other related articles on the PHP Chinese website!

Apachebecamefamousduetoitsopen-sourcenature,modulardesign,andstrongcommunitysupport.1)Itsopen-sourcemodelandpermissiveApacheLicenseencouragedwidespreadadoption.2)Themodulararchitectureallowedforextensivecustomizationandadaptability.3)Avibrantcommunit

Apache's performance and flexibility make it stand out in a web server. 1) Performance advantages are reflected in efficient processing and scalability, which are implemented through multi-process and multi-threaded models. 2) Flexibility stems from the flexibility of modular design and configuration, allowing modules to be loaded and server behavior adjusted according to requirements.

When the Apache 80 port is occupied, the solution is as follows: find out the process that occupies the port and close it. Check the firewall settings to make sure Apache is not blocked. If the above method does not work, please reconfigure Apache to use a different port. Restart the Apache service.

Apache cannot start because the following reasons may be: Configuration file syntax error. Conflict with other application ports. Permissions issue. Out of memory. Process deadlock. Daemon failure. SELinux permissions issues. Firewall problem. Software conflict.

To set up a CGI directory in Apache, you need to perform the following steps: Create a CGI directory such as "cgi-bin", and grant Apache write permissions. Add the "ScriptAlias" directive block in the Apache configuration file to map the CGI directory to the "/cgi-bin" URL. Restart Apache.

There are 3 ways to view the version on the Apache server: via the command line (apachectl -v or apache2ctl -v), check the server status page (http://<server IP or domain name>/server-status), or view the Apache configuration file (ServerVersion: Apache/<version number>).

To restart the Apache server, follow these steps: Linux/macOS: Run sudo systemctl restart apache2. Windows: Run net stop Apache2.4 and then net start Apache2.4. Run netstat -a | findstr 80 to check the server status.

To delete an extra ServerName directive from Apache, you can take the following steps: Identify and delete the extra ServerName directive. Restart Apache to make the changes take effect. Check the configuration file to verify changes. Test the server to make sure the problem is resolved.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.