二级域名是什么?如果有疑惑的同学,先了解下什么叫二级域名,请看二级域名—百度百科 。
这里稍微再简单提一下。
sojson.com
:一级域名
json.sojson.com
:二级域名
www.sojson.com
:二级域名(这里好多人说www是一级域名,我只能和你说,严格讲只有sojson.com这种才是一级域名)。
cdn.www.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
*/
publicvoiddoFilter(finalServletRequest request,finalServletResponse response,finalFilterChain chain)
throwsIOException,ServletException{
finalHttpServletRequest hsRequest =(HttpServletRequest) request;
finalHttpServletResponse 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);
}elseif(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 =
newUrlRewriteWrappedResponse(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 实现多个二级域名
那就先这样。有问题留言或者加群或者私聊我。知无不答。
附件列表
词条内容仅供参考,如果您需要解决具体问题
(尤其在法律、医学等领域),建议您咨询相关领域专业人士。
如果您认为本词条还有待完善,请 编辑
上一篇 齐博CMS分享风格如何把标签内容也分享 下一篇 齐博CMS首页底部标签内容