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

Apache rewrite any subdomain except one to non-www https

· 52CE 我爱测

I'm trying to reduce the amount of 301 redirect chaining on my server so I'd like to combine a subdomain to non-www redirect (except where the subdomain is dev ) with a HTTP to HTTPS redirect (using %{HTTP:X-Forwarded-Proto} ) as…

问题原文

原帖链接 · Matt Parkins · CC BY-SA 4.0

I'm trying to reduce the amount of 301 redirect chaining on my server so I'd like to combine a subdomain to non-www redirect (except where the subdomain is dev) with a HTTP to HTTPS redirect (using %{HTTP:X-Forwarded-Proto}) as the instance is behind a load balancer.

Here's what I have so far in my .htaccess:

# move http to https 
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=301]

# Remove leading www 
RewriteCond %{HTTP_HOST} ^www.example.net [NC]
RewriteRule ^(.*)$ https://example.net/$1 [R=301,L]

There are three problems with my present implementation:

  1. A request to http://www.example.net will have two redirects.

  2. Like most www to non-www redirect examples on this site, it won't redirect ww. or wwww. so my analytics has lots of mistyped subdomains that haven't been redirected.

  3. I'd like to exclude the dev. subdomain from redirection, so http://dev.example.net and its https sibling, as I use dev. for development and release staging.

How should I go about combining this?

已采纳解答

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

1) A request to http://www.example.net will have two redirects.

This can be resolved by simply reversing the two rules. Then www.example.net is redirected to HTTPS in the first redirect, so the HTTP to HTTPS redirect does not need to trigger.

(This does, however, assume you have no intention of implementing HSTS - in which case you would need to keep them as two redirects since redirecting to HTTPS on the same hostname first is a requirement.)

2) Like most www to non-www redirect examples on this site, it won't redirect ww. or wwww. so my analytics has lots of mistyped subdomains that haven't been redirected.

Ordinarily, requests to ww. or wwww. subdomains simply won't resolve, so this is not normally an issue. For this to work you have to have configured a wildcard subdomain in DNS and configured the server to accept such requests.

But this can be accounted for by modifying the regex (snippet) from ^www\. to ^w{2,4}\..

3) I'd like to exclude the dev. subdomain from redirection, so http://dev.example.net and its https sibling, as I use dev. for development and release staging.

This only applies to the HTTP to HTTPS rule, so an additional condition can be applied here to exclude hostnames that start dev..

Bringing the above points together, try the following:

# Remove leading ww, www or wwww (and redirect to HTTPS)
RewriteCond %{HTTP_HOST} ^w{2,4}\.example\.net [NC]
RewriteRule (.*) https://example.net/$1 [R=301,L]

# Move http to https (except dev subdomain)
RewriteCond %{HTTP:Host} !^dev\. [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP:Host}%{REQUEST_URI} [R=301,L]

I've kept your use of HTTP:Host the same (in order to access the Host HTTP request header) in case this is a requirement of the load balancer? Otherwise, it's more common to use the HTTP_HOST server variable here.

The ! prefix on the CondPattern (ie. !^dev\.) negates the regex, so the condition is successful when the Host does not start with dev.. (I assume www.dev. isn't a thing?)

(.*) is the same as ^(.*)$ since the regex is greedy by default.

You will need to clear your browser cache before testing. It is advisable to first test with 302 (temporary) redirects in order to avoid any caching issues.

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

资料来源与适用范围

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

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

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