一、配置区别

A、apache配置:

打开配置文件httpd.conf
1.启用rewrite
# LoadModule rewrite_module modules/mod_rewrite.so 去除前面的 #

2.启用.htaccess
在虚拟机配置项中
AllowOverride None    修改为: AllowOverride All

vim httpd.conf
#[plain] view plain copy
#加载模块
LoadModule rewrite_module modules/mod_rewrite.so
#加载.htaccess
AccessFileName .htaccess
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy All
</Files>
#设置为All,则读取.htaccess内容
[plain] view plain copy
Options FollowSymLinks  
AllowOverride All

B、Nginx配置:不需要单独配置,可直接在.conf文件中编写server{}中的location / { }。可加上include /vhost/rewrite/www.***.cn.conf 等形式来单独设置伪静态文件

注:

1、lnmp.org的lnmp一键安装包的nginx伪静态配置路径在:

/usr/local/nginx/conf/ 
/usr/local/nginx/conf/vhost/域名.conf

2、宝塔nginx环境下伪静态位置:
www/server/panel/vhost/nginx/www.***.cn.conf
www/server/panel/vhost/rewrite/www.***.cn.conf(之所以可以有个rewrite文件夹,是因为server配置中有这个配置的原因:include /www/server/panel/vhost/rewrite/www.***.cn.conf

二、伪静态规则区别

A、apache伪静态(.htaccess文件):服务器有配置文件不可能由我们来改,所以大多情况下要在网站的根目录下建一个.htaccess文件

RewriteEngine on

RewriteRule ^(.*)/ask/([0-9]+)\.html$ $1/ask/index.php?page=$2

B、Nginx伪静态(.conf文件):

rewrite ^(.*)/ask/([0-9]+).html$ $1/ask/index.php?page=$2 last;

说明:Nginx其实就是去掉Ruel 和 括号右边的斜杠就可以,以^开头,以last结尾;$之前的代码是前台要显示的网址,$+空格之后的代码是要匹配的代码,$1匹配的是第一个正则表达式,$2匹配的是第二个正则表达式

 

三、其它

A、宝塔nginx环境下设置伪静态规则: 伪静态规则不要用 location / { } 包裹,需要去掉。但是本人测试加上也没问题。
只是如果在“配置文件”加了这个,“伪静态”处就增加不了自带静态规则的 location / { }
伪静态规则可以放在宝塔的“配置文件”,也可以在“伪静态”里面。

B、Apache mod_rewrite规则重写的标志一览(参考网址:
1) R[=code](force redirect) 强制外部重定向
强制在替代字符串加上http://thishost[:thisport]/前缀重定向到外部的URL.如果code不指定,将用缺省的302 HTTP状态码。
2) F(force URL to be forbidden)禁用URL,返回403HTTP状态码。
3) G(force URL to be gone) 强制URL为GONE,返回410HTTP状态码。
4) P(force proxy) 强制使用代理转发。
5) L(last rule) 表明当前规则是最后一条规则,停止分析以后规则的重写。
6) N(next round) 重新从第一条规则开始运行重写过程。
7) C(chained with next rule) 与下一条规则关联
如果规则匹配则正常处理,该标志无效,如果不匹配,那么下面所有关联的规则都跳过。
8) T=MIME-type(force MIME type) 强制MIME类型
9) NS (used only if no internal sub-request) 只用于不是内部子请求
10) NC(no case) 不区分大小写
11) QSA(query string append) 追加请求字符串
12) NE(no URI escaping of output) 不在输出转义特殊字符
例如:RewriteRule /foo/(.*) /bar?arg=P1%3d$1 [R,NE] 将能正确的将/foo/zoo转换成/bar?arg=P1=zoo
13) PT(pass through to next handler) 传递给下一个处理

C、正则表达式语法:https://www.jb51.net/article/91444.htm

D、各常见cms在ngnix伪静态:https://www.php.cn/php-weizijiaocheng-407634.html

E、使用nginx内置$request_filename变量更容易管理nginx:

F、Nginx使用Location匹配URL进行伪静态:

G、nginx.conf 配置解析之 server配置: