Mobile app version of vmapp.org
Login or Join
RJPawlick198

: 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

@RJPawlick198

Posted in: #Apache #Htaccess #Redirects #UrlRewriting

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]

10.03% popularity Vote Up Vote Down


Login to follow query

More posts by @RJPawlick198

3 Comments

Sorted by latest first Latest Oldest Best

 

@Ravi8258870

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]

10% popularity Vote Up Vote Down


 

@Kaufman445

Try that:

RewriteEngine On

RewriteCond %HTTPS off
RewriteCond Host: (?!www.)(.+)
RewriteRule (.+) www. [I,RP]

RewriteCond %HTTPS on
RewriteCond Host: (?!www.)(.+)
RewriteRule (.+) www. [I,RP]

10% popularity Vote Up Vote Down


 

@Lee4591628

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]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme