: How do I force a www subdomain on both HTTPS and HTTP? For whatever reason I can’t seem to get this right. I’ve looked at many examples on here and on the Apache website. I’m trying
For whatever reason I can’t seem to get this right. I’ve looked at many examples on here and on the Apache website. I’m trying to force example.com instead of example.com on both HTTP and HTTPS, but I am not trying to force use of HTTPS instead of HTTP.
The following code seems to work for all HTTPS connections, but will not cause a redirect for HTTP connections.
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^ www.example.com%{REQUEST_URI} [R=301]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^ www.example.com%{REQUEST_URI} [R=301]
More posts by @RJPawlick198
3 Comments
Sorted by latest first Latest Oldest Best
I've had the same problem, and the below solved it for me. Try it out!
(make sure you replace mydomain.com in the third line with your own)
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Try that:
RewriteEngine On
RewriteCond %HTTPS off
RewriteCond Host: (?!www.)(.+)
RewriteRule (.+) www. [I,RP]
RewriteCond %HTTPS on
RewriteCond Host: (?!www.)(.+)
RewriteRule (.+) www. [I,RP]
Your solution seems right. But let me provide some checkpoints you can perform.
Are HTTP and HTTPS set to point to the same physical directory?
Have you tried asking on server fault?
Can you try using modifiers to check like
Code:
RewriteEngine On
RewriteCond %{HTTPS} = on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^ www.domain.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} != on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^ www.domain.com%{REQUEST_URI} [R=301,L]
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.