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

How do I prevent a rewrite rule from executing for just one subdomain?

· 52CE 我爱测

In the root level .htaccess I have added a condition to rewrite HTTP requests as HTTPS: RewriteEngine On RewriteCond %{HTTPS} !on RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} This applies to the main domain and all subdoma…

问题原文

原帖链接 · Itai · CC BY-SA 3.0

In the root level .htaccess I have added a condition to rewrite HTTP requests as HTTPS:

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

This applies to the main domain and all subdomains. Unfortunately there is still work to do in one of the subdomains in order for it to work with HTTPS, so I would like to insert something into that one's .htaccess file to undo the inherited rewrite rules.

The file must still inherit a lot of other config from the parent domain, so it must only undo that one rewrite rule.

How can I disable this rewrite rule for a particular subdomain?

已采纳解答

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

If the subdomain points to a subdirectory off the main domain's document root then you can simply include a RewriteEngine On directive in that subdirectory's .htaccess file. However, this will override all the mod_rewrite directives in the parent .htaccess file, which may not be desirable (although "other config from the parent domain" will be inherited).

If you have other mod_rewrite directives in the parent .htaccess that you do want to run then you will need to add a condition to this HTTP to HTTPS rule in the parent .htaccess file that specifically excludes the subdomain.

For example:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^subdomain\.
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]

Which says... if the host does not start subdomain. and is not https then...

You normally require the L flag on redirects so processing does not continue through the file (if you have more mod_rewrite directives). Also, this shopuld normally be a 301 (permanent) redirect. If this is the case then change the 302 to 301 when you are sure it's working OK. Although maybe best left as a 302 whilst this is being developed.

Also, no need to capture the URL-path (ie. (.*)) if this is not being used in the RewriteRule substitution (or RewriteCond directives).

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

资料来源与适用范围

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

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

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