建站软件 优化软件 编程软件 网页辅助 站群程序 网站程序 图像处理 资源教程 字体下载 推荐软件


您的位置:首页 > PHP学习 > Nginx常用设置301写法rewrite实例

Nginx常用设置301写法rewrite实例

时间:2019-02-21 10:04:39  来源:免费模板网 作者:风雪 阅读次数 tagsNginxrewrite301

小编最近帮朋友网站切换域名,之前的url地址太长,想简化一下实现去掉url上面的年月日,服务器是Nginx环境怎么写伪静态文件呢

需求如下

我之前的url是http://www.111.com/mwxx/2015/0121/123.html 

网站更换域名改版之后文件生成的路径去掉了年月日,也就是http://www.222.com/mwxx/123.html

服务器环境是Nginx 怎么去写这个伪静态规则呢 就是访问http://www.111.com/mwxx/2015/0121/123.html 会跳转到http://www.222.com/mwxx/123.html

这里我们就要先了解一下伪静态语法

^(.*)mwxx/([0-9]+)/([0-9]+)/([0-9]+)\.html$

那么 然后是 $1/mwxx/$4.html  

 $1 代表匹配的第一个括号的内容
$4是第四个括号的内容 

总结就是以下写法

rewrite ^/resume/grjl/([0-9]+)/([0-9]+)\.html$ http://www.222.com/mwxx/$2.html permanent;

根据你的实际来变化

扩展一下相关知识

正则表达式匹配,其中:

 

* ~ 为区分大小写匹配

 

* ~* 为不区分大小写匹配

 

* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配

 

文件及目录匹配,其中:

 

* -f和!-f用来判断是否存在文件

 

* -d和!-d用来判断是否存在目录

 

* -e和!-e用来判断是否存在文件或目录

 

* -x和!-x用来判断文件是否可执行

flag标记有:

 

* last 相当于Apache里的[L]标记,表示完成rewrite

 

* break 终止匹配, 不再匹配后面的规则

 

* redirect 返回302临时重定向 地址栏会显示跳转后的地址

 

* permanent 返回301永久重定向 地址栏会显示跳转后的地址

我们看实际代码

1、将www.freemoban.com/connect 跳转到connect.freemoban.com

rewrite ^/connect$ http://connect.freemoban.com permanent;
rewrite ^/connect/(.*)$ http://connect.freemoban.com/$1 permanent;

2、将connect.freemoban.com 301跳转到www.freemoban.com/connect/ 

if ($host = "connect.freemoban.com"){
rewrite ^/(.*)$ http://www.freemoban.com/connect/$1 permanent;
}

3、freemoban.com 跳转到www.freemoban.com

if ($host != 'www.freemoban.com' ) { 
rewrite ^/(.*)$ http://www.freemoban.com/$1 permanent; 
}

4、www.freemoban.com/category/123.html 跳转为 category/?cd=123

rewrite "/category/(.*).html$" /category/?cd=$1 last;

5、www.freemoban.com/admin/ 下跳转为www.freemoban.com/admin/index.php?s=

if (!-e $request_filename){
rewrite ^/admin/(.*)$ /admin/index.php?s=/$1 last;
}

6、在后面添加/index.php?s=

if (!-e $request_filename){
    rewrite ^/(.*)$ /index.php?s=/$1 last;
}

7、www.freemoban.com/xinwen/123.html  等xinwen下面数字+html的链接跳转为404

rewrite ^/xinwen/([0-9]+)\.html$ /404.html last;

8、http://www.freemoban.com/news/radaier.html 301跳转 http://www.freemoban.com/strategy/

rewrite ^/news/radaier.html http://www.freemoban.com/strategy/ permanent;

9、重定向 链接为404页面

rewrite http://www.freemoban.com/123/456.php /404.html last;

10、禁止htaccess

location ~//.ht {
      deny all;
}

11、可以禁止/data/下多级目录下.log.txt等请求;

location ~ ^/data {
     deny all;
}

12、禁止单个文件

location ~ /www/log/123.log {
      deny all;
}

13、http://www.freemoban.com/news/activies/2014-08-26/123.html 跳转为 http://www.freemoban.com/news/activies/123.html

rewrite ^/news/activies/2014\-([0-9]+)\-([0-9]+)/(.*)$ http://www.freemoban.com/news/activies/$3 permanent;

14、nginx多条件重定向rewrite

如果需要打开带有play的链接就跳转到play,不过/admin/play这个不能跳转

 

if ($request_filename ~ (.*)/play){ set $payvar '1';}
if ($request_filename ~ (.*)/admin){ set $payvar '0';}
if ($payvar ~ '1'){
        rewrite ^/ http://play.freemoban.com/ break;
}

15、http://www.freemoban.com/?gid=6 跳转为http://www.freemoban.com/123.html

if ($request_uri ~ "/\?gid\=6"){return  http://www.freemoban.com/123.html;}

常见的需求也就这多,快去试试伪静态的魅力吧

本文地址:https://www.freemoban.com/php/2019/0221/3071.html

猜你喜欢
栏目推荐
模板推荐

Copyright:www.freemoban.com 免费模板网 All Rights Reserved 网站备案:辽ICP备19014872号-2   辽公网安备 21010602000376号  辽公网安备:42900402000182号

免责声明:本站部分资源来自互联网收集,版权归原创者所有,如果侵犯了你的权益,我们会及时删除侵权内容,联系QQ:1615187561 谢谢合作!