大网数据广告活动
服务器运维问答

Apache2 wildcard alias

· 52CE 我爱测

I have the following wildcard vHost: <VirtualHost *:80> ServerAdmin [email omitted] ServerName autodiscover.*.* ServerAlias autoconfig.*.* RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 […

问题原文

原帖链接 · Tim Altgeld · CC BY-SA 4.0

I have the following wildcard vHost:

<VirtualHost *:80>
    ServerAdmin [email omitted]
    ServerName autodiscover.*.*
    ServerAlias autoconfig.*.*

    RewriteEngine On 
    RewriteCond %{HTTPS} !=on 
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
</VirtualHost>

But my RewriteRule is wrong.

For Example:

autoconfig.testdomain.de gets mapped to https://autoconfig.testdomain.de

What I want:

  • autoconfig.*.* gets mapped to https://autoconfig.maindomain.de

  • autodiscover.*.* gets mapped to https://autodiscover.maindomain.de

I want to parse only the subdomain part in %{HTTP_HOST} to subdomain.example.de, but only if the subdomain part is either autoconfig or autodiscover

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

Try the following instead:

RewriteCond %{HTTP_HOST} ^([^.]+)
RewriteRule ^/(.*) https://%1.maindomain.de/$1 [R=301,L]

There's no need to check %{HTTPS} !=on since you are in the vHost for port 80.

The RewriteCond directive captures just the subdomain from the requested hostname (which can only be autoconfig or autodiscover from the vHost config stated). This is then referenced using the %1 backreference in the RewriteRule substitution string.

Alternatively, you can explicitly check for only the autoconfig or autodiscover subdomains in the condition:

RewriteCond %{HTTP_HOST} ^(autoconfig|autodiscover)\.

You need to clear your browser cache before testing, since the erroneous 301 (permanent) redirects will have been cached by the browser. Test with 302 (temporary) redirects to avoid caching issues.

ServerName autodiscover.*.*

I guess you must be on an older version of Apache, since you can't use wildcards in the ServerName directive on newer versions of Apache (it would otherwise create an ambiguity, since the ServerName is used to create self-referenctial URLs in certain scenarios). You should only specify wildcards in the ServerAlias directive.

This should be changed to something like:

ServerName autodiscover.something.something
ServerAlias autodiscover.*.*
ServerAlias autoconfig.*.*

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-02-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。