最近在做一個自建網站平臺(
http://www.yowob.com ),其他涉及到為用戶提供一個二級域名功能,實現方法如下:
(1)首先要在域名服務商端做一個泛域名解析. 我用的是godaddy.com,就新建一個A記錄(host=*,point to指向我的服務器的ip),這樣所有二級域名都會轉到我的服務器來了
(2)接著在web.xml配一個自已寫的域名過濾器,
<filter>
<filter-name>URLFilter</filter-name>
<filter-class>com.yowob.commons.URLFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>URLFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
過濾器的代碼如下。這里先將二級域名和全球域名和用戶ID的映射,保存在一個數據表里, 然后訪問進來時對地址做一個判斷, 再取出對應的用戶ID. 再轉一下就行了. 我的靜態文件都在static目錄,所以還加了一個static的判斷。
比如:
http://time.you.com/board/21 用time對應用戶ID為6,則訪問效果有
http://www.you.com/6/board/21相同, 不過地址欄還是顯示
http://time.you.com/board/21。
再比如:htttp://www.userdomain.com/board/21,這個是用戶ID為6的全球域名,訪問效果也和上面一樣。
package com.yowob.commons;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.yowob.Constants;
import com.yowob.dao.SiteDAO;
import com.yowob.dto.SiteDTO;
public class URLFilter implements Filter {
private static final Log log = LogFactory.getLog(URLFilter.class);
private static final String DOMAIN_END = "." + Constants.DOMAIN; //.you.com
private static final Map<String, Long> NAME_MAP = new HashMap<String, Long>();
private static final Map<String, Long> DOMAIN_MAP = new HashMap<String, Long>();
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("------------------------------init");
SiteDAO siteDAO = new SiteDAO();
List<SiteDTO> list = siteDAO.getAll();
for (SiteDTO siteDTO : list) {
String name = siteDTO.getName();
if (StringUtils.isNotEmpty(name)) {
NAME_MAP.put(name, siteDTO.getId());
}
String domain = siteDTO.getDomain();
if (StringUtils.isNotEmpty(domain)) {
DOMAIN_MAP.put(domain, siteDTO.getId());
}
}
}
public static void updateName(String oldValue, String newValue, Long siteId) {
if (StringUtils.equals(oldValue, newValue)) {
return;
}
if (StringUtils.isNotEmpty(oldValue)) {
NAME_MAP.remove(oldValue);
}
if (StringUtils.isNotEmpty(newValue)) {
NAME_MAP.put(newValue, siteId);
}
}
public static void updateDomain(String oldValue, String newValue, Long siteId) {
if (StringUtils.equals(oldValue, newValue)) {
return;
}
if (StringUtils.isNotEmpty(oldValue)) {
DOMAIN_MAP.remove(oldValue);
}
if (StringUtils.isNotEmpty(newValue)) {
DOMAIN_MAP.put(newValue, siteId);
}
}
@Override
public void destroy() {
log.info("------------------------------destroy");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
String requestURI = request.getRequestURI();
String serverName = request.getServerName().toLowerCase();
String realURI = getRealRequestURI(serverName, requestURI);
request.getRequestDispatcher(realURI).forward(request, response);
}
private String getRealRequestURI(String serverName, String requestURI) {
if (Constants.WWW_DOMAIN.equals(serverName) || requestURI.startsWith("/static/") || Constants.DOMAIN.equals(serverName)) {
return requestURI;
}
if (serverName.endsWith(DOMAIN_END)) {
String secondDomain = serverName.substring(0, serverName.indexOf("."));
//網站id
if (NumberUtils.isNumber(secondDomain))
return getURI(secondDomain, requestURI);
//網站英文名
Long siteId = NAME_MAP.get(secondDomain);
if (siteId == null) {
//保留的二級域名
if (Constants.isPrivateSecondDomain(secondDomain)) {
return requestURI;
}
return "/message?msg=不存在二級域名" + secondDomain;
// throw new RuntimeException("do not exist second domain: " + secondDomain);
}
return getURI(siteId + "", requestURI);
}
//域名
Long siteId = DOMAIN_MAP.get(serverName);
if (siteId == null) {
return requestURI;
} else {
return getURI(siteId + "", requestURI);
}
}
private static String getURI(String siteId, String requestURI) {
if (requestURI.equals("/")) {
return "/" + siteId;
} else {
return "/" + siteId + requestURI;
}
}
}
其他一些小技巧:
(1)為了便于本機測試,可以修改windows 的hosts文件。我的如下:
127.0.0.1 localhost
127.0.0.1 you.com
127.0.0.1 www.you.com
127.0.0.1 time1.you.com
127.0.0.1 time2.you.com
127.0.0.1 6.you.com
127.0.0.1 www.bobo.com
(2)web頁的各種地址則要注意相對路徑的問題。主要考慮
首先設置<base />,或<base />其中地址最后要加一個/
然后頁面中其他地址前面不加/,就是相對地址(以地址欄為基礎)。加上/,則是絕對地址。