核心标记说明:
last:重写完成,使用重写后URL继续匹配后续location;
break:终止当前location匹配,不再向下匹配;
redirect:302临时跳转;permanent:301永久跳转;
# ThinkPHP通用伪静态 rewrite '(!css)(.*).html' /index.php?s=$1 last; rewrite '(!Public)(.*).html' /index.php?s=$1 last; # 404错误页配置 error_page 404 500 /error.html; error_page 404 = https://域名/404.html;
标记说明:
1) R=[code] 强制外部重定向,302/301;
2) F 拒绝访问,返回403;
3) L 最后一条规则,停止后续匹配;
4) NC 忽略大小写;QSA 追加原有GET参数;
# ThinkPHP Apache伪静态
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*).html$ index.php?s=$1 [L,QSA,NC]
</IfModule>
基于XML rewrite模块,适配Windows服务器,无需额外组件
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<outboundRules>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html/" />
</preCondition>
</preConditions>
</outboundRules>
<rules>
<clear/>
<rule name="thinkphp" stopProcessing="true">
<match url="^(?!css)(.*).html$" />
<action type="Rewrite" url="/index.php?s={R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Go语言服务,语法极简,原生HTTPS,规则使用rewrite / redir
example.com {
root * ./public
php_fastcgi 127.0.0.1:9000
# ThinkPHP伪静态
rewrite * /index.php?s={path} if {path} not_match ^/(css|js|img)
file_server
}
完全兼容Nginx语法,可嵌入Lua脚本实现复杂业务转发
location ~ \.html$ {
rewrite '(!css)(.*).html' /index.php?s=$1 last;
content_by_lua_block {
ngx.var.args = ngx.var.args .. "&from=openresty"
}
}
100%兼容Apache .htaccess规则,无需修改即可直接迁移,性能优于Apache
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*).html$ index.php?s=$1 [L,NC]