问题原文
原帖链接 · 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:
A request to
http://www.example.netwill have two redirects.Like most www to non-www redirect examples on this site, it won't redirect
ww.orwwww.so my analytics has lots of mistyped subdomains that haven't been redirected.I'd like to exclude the
dev.subdomain from redirection, sohttp://dev.example.netand itshttpssibling, as I usedev.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.netwill 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.orwwww.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, sohttp://dev.example.netand itshttpssibling, as I usedev.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.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
