加入收藏 | 设为首页 | 会员中心 | 我要投稿 网站开发网_安阳站长网 (https://www.0372zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长百科 > 正文

nginx rewrite规则实例讲解

发布时间:2020-12-26 16:19:03 所属栏目:站长百科 来源:网络整理
导读:副标题#e# 一.正则表达式匹配,其中: * ~ 为区分大小写匹配 * ~* 为不区分大小写匹配 * !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 二.文件及目录匹配,其中: * -f和!-f用来判断是否存在文件 * -d和!-d用来判断是否存在目录 * -e和!-e用来判断


例如:允许指定的域名访问本站,其他的域名一律转向www.linuxidc.net
Apache:
RewriteCond %{HTTP_HOST} !^(.*?).aaa.com$[NC]
RewriteCond %{HTTP_HOST} !^localhost$
RewriteCond %{HTTP_HOST}!^192.168.0.(.*?)$
RewriteRule ^/(.*)$ http://www.linuxidc.net[R,L]


Nginx:
if( $host ~* ^(.*).aaa.com$ )
{
set $allowHost ‘1’;
}
if( $host ~* ^localhost )
{
set $allowHost ‘1’;
}
if( $host ~* ^192.168.1.(.*?)$ )
{
set $allowHost ‘1’;
}
if( $allowHost !~ ‘1’ )
{
rewrite ^/(.*)$ http://www.linuxidc.netredirect ;
}

?

常见nginx rewrite实例

1、php常见框架重定向,tag功能重定向

location / {
? root /app/html;
? rewrite ^/tag/([a-z0-9]+)/$ /index.php?m=tag&c=tag&id=$1 last;
? rewrite ^/(.*) /index.php?_ROUTE_=$1 last;
? index index.html index.php;
}

(编辑:网站开发网_安阳站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!