当前位置: 移动技术网 > IT编程>开发语言>PHP > 重定向漫谈​

重定向漫谈​

2018年09月21日  | 移动技术网IT编程  | 我要评论

1. 相关知识点

nginx重定向规则(apache 的简介);
nginx两种跳转:显式跳转,隐式跳转;
伪静态;
同源策略;
 
2.我的重定向主要应用
1: 定向到   ,给网站加小绿锁;便于百度的收录,权重集中到一个域名;
2: 隐式到 ,看上去我有个专业的技术博客。
 
3.源码
一个字就是干!下面贴出nginx 配置 和   的源文件。(个人网站被攻击也没啥损失,自己印象云,git都有备份,还请大哥高抬贵手!)
zouzhenzhong.conf
#主站
server {
    #这里是ssl 对应配置,从对应的云服务器copy demo对应填写即可
    listen 443 ssl;
    server_name  ;
    ssl on;
    ssl_certificate /etc/ssl/tencent/;
    ssl_certificate_key /etc/ssl/tencent/;
    ssl_session_timeout 5m;
    ssl_protocols tlsv1 tlsv1.1 tlsv1.2; #按照这个协议配置
    ssl_ciphers ecdhe-rsa-aes128-gcm-sha256:high:!anull:!md5:!rc4:!dhe;#按照这个套件配置
    ssl_prefer_server_ciphers on;
    
    # 站点的数据能够让其他任何网站拉取,展示;担心安全问题可以使用jsonp,数据源带token的形式。
    add_header access-control-allow-origin *;
    
    #url 隐式跳转:  显示的是的内容, 但是url 显示的是
    #注意关键词proxy_pass
    location ~* \.html$ {
        rewrite ^/([\d]+)\.html$ /article/detail/$1 break;
        proxy_pass ;
    }
    
    location / {
        root   /home/yiiblog/frontend/web;
        index   index.php;
        if (!-e $request_filename){
            rewrite ^/(.*) /index.php last;
        }
    }
    
    #没有下面 cgi(common gateway interface)将无法解析php
    location ~ .php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        root   /home/yiiblog/frontend/web;
        fastcgi_param script_filename $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    
    #对应资源,不区分大小写,301强跳,便于cdn
    location ~* \.(png|jpg|jpeg||gif|js|css|woff2|eot|ttf|woff|svg|otf)$  {
        rewrite ^/(.*)$  permanent;
    }
}
 
server {
    #注意https 监听的一定是443端口;所以  监听了 80端口和443两个端口;
    listen  80 default_server;
    server_name ;
    #重定向到www
    rewrite ^/(.*)$  permanent;
}
 
tec_zouzhenzhong.conf
server {
    listen 443 ;
    server_name ;
    ssl on;
    ssl_certificate /etc/letsencrypt/live/;
    ssl_certificate_key /etc/letsencrypt/live/;
    ssl_session_timeout 5m;
    ssl_protocols tlsv1 tlsv1.1 tlsv1.2; #按照这个协议配置
    ssl_ciphers ecdhe-rsa-aes128-gcm-sha256:high:!anull:!md5:!rc4:!dhe;#按照这个套件配置
    ssl_prefer_server_ciphers on;
    
    # 所有的访问  都会隐式重定向到代理的/article/listshow?menu_id=101
    location / {
        rewrite ^/(.*)$ /article/listshow?menu_id=101 break;
        proxy_pass ;
    }
    #location ~ .php$  没有做cgi的配置, 并不需要解析php  
}
 
server {
    listen  80;#这个不能省略啊
    server_name  ;
    rewrite ^(.*)$ https://${server_name}$1 permanent;
}
 
4.知识点解析
nginx重定向规则
^/([\d]+)\.html$   /article/detail/$1
^/ 就理解为开始就好(还有很多形式,就这样用就行)。
使用括号 () 通过正则匹配标记要截取的内容 ,对应成变量$1,$2...
       location / 通用匹配,任何未匹配到其它location的请求都会匹配到,相当于switch中的default。
参考资料:  
 
nginx两种跳转
显式跳转:访问的url跳转
location ~* \.(png|jpg|jpeg||gif|js|css|woff2|eot|ttf|woff|svg|otf)$  {
    rewrite ^/(.*)$  permanent;
}
隐式跳转:访问的url 不跳,但是内容已经跳了。
注意关键词:proxy_pass ;(吐槽下,网上黏贴复制的文章错的一塌糊涂都还各种转载....)
  location ~* \.html$ {
        rewrite ^/([\d]+)\.html$ /article/detail/$1 break;
        proxy_pass ;
 }
注意: service nginx restart 前进行 nginx -t 检查;配置文件记得备份;
 
伪静态
伪静态 :优点:便于seo ; 缺点:会增加一点cpu开销(多一次跳转); 优点的作用 > 缺点。
 
同源策略
对应的service name  对那些host开放;比如:  access-control-allow-origin * , 可以给其他网站加载,嵌套渲染等;
 
5.其他
apache 重定向  
kimma.conf :  中 静态文件跳 
<virtualhost *:80>
        serveradmin 
        documentroot /var/www/html
        header set access-control-allow-origin “*”
        <ifmodule mod_rewrite.c>
                rewriteengine on
                rewritecond %{http_host} .*$ [nc]
                rewriterule "^/(.*)\.(png|jpg|ttf|woff|woff2|jpeg|gif|gif|js|css|woff2|eot|ttf|woff|svg|otf|txt)$" ""  [l,r=301]
        </ifmodule>
</virtualhost>
apache .htaccess   跳 
rewriteengine on
rewritecond %{http_host} ^ [nc]
rewriterule ^/(.)*$   [l,r=301]
header set cache-control "max-age=2592000"
注意:重启apche: apachectl restart 前检查 apche: apachectl -t
 
文章来源:

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网