单个项目多个二级域名简单实现思路

JSON 2018-11-22 13:59:35 32635

二级域名是什么?如果有疑惑的同学,先了解下什么叫二级域名,请看二级域名—百度百科

这里稍微再简单提一下。

sojson.com:一级域名

json.sojson.com:二级域名

www.sojson.com:二级域名(这里好多人说www是一级域名,我只能和你说,严格讲只有sojson.com这种才是一级域名)。

cdn.sojson.com:二级域名

img.cdn.www.sojson.com:三级域名(以此类推,有三级、四级、五级...)

sojson.com/blog/:这里的 /blog/ 它只是一个目录和几级域名没有任何关系。

但有的人把一级除外的都称为二级域名,也勉强可以。

Java 实现多个二级域名

下面进入主题,单个项目多个二级域名简单实现思路。



其实也简单。如上图,也就是把二级域名,转换成目录即可, SpringMVC  支持这种结构,可以对Controller动态添加目录。

不过我是采用Filter方式实现,就是在 SpringMVC  org.springframework.web.servlet.DispatcherServlet之前拦截,处理地址即可。

我现在没有现成的代码,挺简单的,我是因为还有伪静态要处理,所以我在 urlrewrite  Filter里处理的。如下代码。


/**
 * The main method called for each request that this filter is mapped for.
 *
 * @param request  the request to filter
 * @param response the response to filter
 * @param chain    the chain for the filtering
 * @throws IOException
 * @throws ServletException
 */
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
        throws IOException, ServletException {

	
	final HttpServletRequest hsRequest = (HttpServletRequest) request;
        final HttpServletResponse hsResponse = (HttpServletResponse) response;
	
	String uri = hsRequest.getRequestURI();
	String url = hsRequest.getRequestURL().toString();
	LoggerUtils.fmtDebug(getClass(), "进入UrlRewriteFilter,uri[%s]",uri);
	LoggerUtils.fmtDebug(getClass(), "进入UrlRewriteFilter,url[%s]",url);
	
	if(uri.matches("/doc/([\\d]+)(\\.html)?$") || uri.matches("/blog/([\\d]+)(\\.html)$") ){
		uri = uri.replace("doc", "blog").replace(".html", "");
		hsResponse.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);   
		hsResponse.setHeader("Location",uri);  
		
	}else if(uri.equals("/doc/index.html")){
			hsResponse.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);   
			hsResponse.setHeader("Location","/blog/");  
			
		}else{
    	if(url.endsWith(".shtml") || url.endsWith(".txt")){
    		LoggerUtils.fmtDebug(getClass(), "匹配为不需要静态化的地址,继续下一个拦截器!,uri[%s]",uri);
    		chain.doFilter(hsRequest, response);
    	}else{
    		
    	
	    	if((uri.startsWith("/demo/") && !uri.equals("/demo/index.html"))){
	    		LoggerUtils.fmtDebug(getClass(), "匹配Demo地址成功,继续下一个拦截器!,uri[%s]",uri);
	    		chain.doFilter(hsRequest, response);
	    	}else{
	    		LoggerUtils.fmtDebug(getClass(), "匹配伪静态地址成功,进行地址伪静态!,uri[%s]",uri);
		        UrlRewriter urlRewriter = getUrlRewriter(request, response, chain);
		
		       
		        UrlRewriteWrappedResponse urlRewriteWrappedResponse = 
		        	new UrlRewriteWrappedResponse(hsResponse, hsRequest, urlRewriter);
		
		        // check for status request
		        if (statusEnabled && statusServerNameMatcher.isMatch(request.getServerName())) {
		            //String uri = hsRequest.getRequestURI();
		            if (log.isDebugEnabled()) {
		                log.debug("checking for status path on " + uri);
		            }
		            String contextPath = hsRequest.getContextPath();
		            if (uri != null && uri.startsWith(contextPath + statusPath)) {
		                showStatus(hsRequest, urlRewriteWrappedResponse);
		                return;
		            }
		        }
		
		        boolean requestRewritten = false;
		        if (urlRewriter != null) {
		
		            // process the request
		            requestRewritten = urlRewriter.processRequest(hsRequest, urlRewriteWrappedResponse, chain);
		        } else {
	            	    LoggerUtils.debug(getClass(),
	            			"urlRewriter engine not loaded ignoring request (could be a conf file problem)");
		        }
		
		        // if no rewrite has taken place continue as normal
		        if (!requestRewritten) {
		            chain.doFilter(hsRequest, urlRewriteWrappedResponse);
		        }
		    	
		    }
    	      }
       }
}

就用这种匹配方式,然后取到二级域名,然后重定向

hsResponse.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);   
hsResponse.setHeader("Location",uri);  

或者用response直接重定向都可以。具体看下一篇博客。

Java 实现多个二级域名 

那就先这样。有问题留言或者加群或者私聊我。知无不答。


版权所属:SO JSON在线解析

原文地址:https://www.sojson.com/blog/76.html

转载时必须以链接形式注明原始出处及本声明。


如果本文对你有帮助,那么请你赞助我,让我更有激情的写下去,帮助更多的人。

关于作者
一个低调而闷骚的男人。
相关文章
Java 实现多个二级域名访问同一个Tomcat(系统)。
Shiro 通过配置Cookie 解决多个二级域名的单点登录问题。
Nginx 跳转到www二级域名域名重定向配置方法。
正则从URL中提取一域名,支持大部分域名
Java 正则获取一域名
Javascript 判断域名合法性,JS域名格式检测
Java获取域名,Java从URL地址中获取域名,Java从Request 获取域名
根据域名获取IP地址,Java 获取域名的IP地址
IE、Firefox对同一域名访问并发限制,及解决优化方案
Java爬取百度云观测对网站的检测数据,获取子域名域名的安全信息
最新文章
PHP变量剖析 17
SQL全外连接剖析 137
PHP面向对象编程最详讲解和例子 234
PHP用户定义函数详细讲解 48
SQL交叉连接剖析 102
SQL自然连接剖析 147
springboot启动原理 245
SQL右连接【RIGHT JOIN】详解及图解 450
SQL左链接【LEFT JOIN】详解及图解 357
SQL非等值连接剖析 262
最热文章
最新MyEclipse8.5注册码,有效期到2020年 (已经更新) 679284
苹果电脑Mac怎么恢复出厂系统?苹果系统怎么重装系统? 674561
免费天气API,全国天气 JSON API接口,可以获取五天的天气预报 599041
免费天气API,天气JSON API,不限次数获取十五天的天气预报 565448
Jackson 时间格式化,时间注解 @JsonFormat 用法、时差问题说明 551726
我为什么要选择RabbitMQ ,RabbitMQ简介,各种MQ选型对比 509186
Elasticsearch教程(四) elasticsearch head 插件安装和使用 479645
Jackson 美化输出JSON,优雅的输出JSON数据,格式化输出JSON数据... ... 262845
Java 信任所有SSL证书,HTTPS请求抛错,忽略证书请求完美解决 244092
Elasticsearch教程(一),全程直播(小白级别) 225127
支付扫码

所有赞助/开支都讲公开明细,用于网站维护:赞助名单查看

查看我的收藏

正在加载... ...