apache - 二级域名映射到一级域名的url

【字号: 日期:2022-07-30浏览:51作者:雯心

问题描述

假设一级域名是 taobao.com每个店铺有一个二级域名,有一个店铺叫宝宝,他的域名是 baobao.taobao.com每个店铺通过二级域名访问,转向 taobao.com/shop/baobao

也就是说有大量的店,每个店有一个二级域名,假设域名是xxx.taobao.com需要转向 taobao.com/shop/xxx

DNS的* A 记录都指向这台机器。如果使用apache mod_rewrite 实现的话,配置如何写?

另还有其他方式没?

问题解答

回答1:

Nginx的写法大概是这样的:

server { listen 80; server_name xxx.taobao.com; if ( $http_host ~* “^(.*).taobao.com$”) { set $domain $1; rewrite ^(.*) http://taobao.com/$domain/ break; }}

Apache类似:

RewriteRule ^/([-a-z0-9]+).taobao.com/?$ http://taobao.com/$1/ [L]回答2:

目的 当访问abc.test.com跳转到www.test.com/test/abc ,浏览器地址会显示abc.test.com地址。方法在apache的配置文件里新增虚拟主机NameVirtualHost :80ServerName www.test.comServerAlias *.test.comDocumentRoot /var/www/testrewriteengine onrewritecond %{HTTP_HOST} [^.]+.test.com$rewriterule ^(.+) %{HTTP_HOST}$1 [C]rewriterule ([^.]+).test.com(.) /test/$1$2 #/test是www.test.com/后边的目录

另外可以打开一下你的debug,看一下日志么?

回答3:

我增加了apache配置:RewriteRule ^/([-a-z0-9]+).xiaocaowangluo.com/?$ http://xiaocaowangluo.com/shop/$1/ [L]

这里有两个连接 http://xiaocaowangluo.com/shop/test/ http://test.xiaocaowangluo.com/ 增加了这个配置。

http://test.xiaocaowangluo.com/ 还是没有办法跳到 http://xiaocaowangluo.com/shop/test/ 是不是要需要增加其他的

另我用RewriteEngine on RewriteCond %{HTTP_HOST} ^[^.]+.xiaocaowangluo.com$RewriteRule ^(.+) %{HTTP_HOST}$1 [C]RewriteRule ^([^.]+).xiaocaowangluo.com(.*) /www/xiaocaowangluo/shop/$1$2

这个配置也不行,求大神。

回答4:

apache 可以这样写

RewriteRule ^/([-a-z0-9]+).yourdomain.com/?$ http://taobao.com/$1/ [L]

Appconfig:http://www.aips.me/sae-binding-domain-subdirectory.html

相关文章: