大网数据广告活动
网站运营与故障问答

HTTP to HTTPS redirect for a specific domain through web.config

· 52CE 我爱测

I'm trying to do a HTTP to HTTPS redirect on a GoDaddy web hosting platform. I have the Windows hosting platform and I'm editing the web.config file. There are multiple domains under this hosting account, and I'm trying to do the…

问题原文

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

I'm trying to do a HTTP to HTTPS redirect on a GoDaddy web hosting platform. I have the Windows hosting platform and I'm editing the web.config file. There are multiple domains under this hosting account, and I'm trying to do the redirect for only ONE domain and its subdomains.

Example:

  • 5 domains: domain1.example , domain2.example, etc... and each domain may have subdomains

Desired behaviour:

  • *.domain1.example redireced to https://*.domain1.example (with or without www)
  • include a wildcard in front of the domain, to handle any sub-domains

This is what I started with, which will redirect every domain from HTTP to HTTPS, and it works properly:

<rule name="HTTP to HTTPS redirect" stopProcessing="true"> 
    <match url="(.*)" /> 
        <conditions> 
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
        </conditions> 
    <action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>  

Now I tried to make it for a specific domain only, but I really have no idea how to write this myself. Instead I have put together some stuff I found online. I find all the +?\ and A-Za quite confusing.

        <rule name="HTTP to HTTPS redirect" stopProcessing="true"> 
            <match url="(https?:\/\/(.+?\.)?domain1\.example(\/[A-Za-z0-9\-\._~:\/\?#\[\]@!$&'\(\)\*\+,;\=]*)?)" /> 
                <conditions> 
                    <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                </conditions> 
            <action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
        </rule>   

But this results in a 500 (internal server) error.

已采纳解答

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

<match url="(https?:\/\/(.+?\.)?domain1\.example

I believe the url attribute matches URL-paths only, not the absolute URL, so this will never match. But if it did match, it's also matching https, which is not what you require.

Try just adding another condition to your original rule that checks the HTTP_HOST for the specific domain. For example:

<add input="{HTTP_HOST}" pattern="^([a-z]+\.)?domain1\.example$" />

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

资料来源与适用范围

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

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

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