我有一个客户的规格。给出了一个apache web服务器的重写模块,找到了一种基于主机头根域的动态重定向方法。例如,如果请求来自www.example1.com,则重定向到example1.customersite.com同样的事情与多个级别的子域(最多3个),如my.redirect.to.example2.com应重定向到example2.customersite.com。这可以通过重写模块来实现吗?
www.example1.com
example1.customersite.com
my.redirect.to.example2.com
example2.customersite.com
kiayqfof1#
我假设这是任何URL路径,这应该通过重定向保留。因此,格式为subdomain.<example>.com/<url-path>的URL将被重定向到<example>.customersite.com/<url-path>。你可以使用mod_rewrite做如下事情:
subdomain.<example>.com/<url-path>
<example>.customersite.com/<url-path>
RewriteEngine OnRewriteCond %{HTTP_HOST} ^(?:[^.]+\.){1,3}(example)\.com [NC]RewriteRule ^ https://%1.customersite.com%{REQUEST_URI} [R=302,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:[^.]+\.){1,3}(example)\.com [NC]
RewriteRule ^ https://%1.customersite.com%{REQUEST_URI} [R=302,L]
HTTP_HOST服务器变量(使用语法%{HTTP_HOST}访问)包含所请求的Host头的值。这只允许1到3个子域。域顶点(无子域)或超过3个将不会被重定向。%1反向引用包含前面 condition 中捕获子模式的值。在这种情况下,它只包含字符串“example”,以保存重复。请注意,这是一个302(临时)重定向。
HTTP_HOST
%{HTTP_HOST}
Host
%1
1条答案
按热度按时间kiayqfof1#
我假设这是任何URL路径,这应该通过重定向保留。因此,格式为
subdomain.<example>.com/<url-path>
的URL将被重定向到<example>.customersite.com/<url-path>
。你可以使用mod_rewrite做如下事情:
HTTP_HOST
服务器变量(使用语法%{HTTP_HOST}
访问)包含所请求的Host
头的值。这只允许1到3个子域。域顶点(无子域)或超过3个将不会被重定向。
%1
反向引用包含前面 condition 中捕获子模式的值。在这种情况下,它只包含字符串“example”,以保存重复。请注意,这是一个302(临时)重定向。