Results 1 to 11 of 11
  1. #1
    Join Date
    Jul 2014
    Posts
    42

    Redirect users to https://www

    Hello,

    How do I redirect all non https and non-www users to https://www.mydomain.com? I use apache on centos

    Thanks so much in advance

    Sal

  2. #2
    Using apache you can create a virtual host. I'm not sure if its considered best practice though, but it works.
    <VirtualHost *:80>
    ServerName domain.com
    Redirect permanent / https://www.domain.com
    </VirtualHost>

  3. #3
    Try putting the code below in a .htaccess file in the document root. Make sure AllowOverride is enabled in the Apache configuration file.

    Code:
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    █‏ █‏ Phil Kinsley - NotionHosting
    █‏ █‏ cPanel | SSD | CloudLinux | LiteSpeed | R1Soft Backups and more!
    █‏ █‏ Shared Hosting | Managed Hosting | Self/Fully Managed VPS | Dedicated Servers | Discounted Domains

  4. #4
    Join Date
    Jul 2014
    Posts
    42

    Tried

    Quote Originally Posted by PhilK View Post
    Try putting the code below in a .htaccess file in the document root. Make sure AllowOverride is enabled in the Apache configuration file.

    Code:
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    Just tried it but got below issues

    domain.com goes to https://domain.com but not https://www.domain.com

    https://domain.com shows certificate warning as certificate is only for www.domain.com

  5. #5
    Join Date
    Jul 2014
    Posts
    42

    Thanks

    Quote Originally Posted by murphyslaw4267 View Post
    Using apache you can create a virtual host. I'm not sure if its considered best practice though, but it works.
    <VirtualHost *:80>
    ServerName domain.com
    Redirect permanent / https://www.domain.com
    </VirtualHost>
    I know folks generally use htaccess for this so i'll first give it a try. Need to look up how to create virtual host too

  6. #6
    This should get the job done, add it to your .htaccess file:

    Code:
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^$
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    Also if your certificate works fine on www it should work just as fine on non-www, it could just be a cache issue on your end seeing the cert error on non-www.
    Uptime Monitor - Minimize your downtime by being the first to know about it!

    Blacklist Monitor - Are any of your IPs or Domains blacklisted? Find out before it gets to affect you or your clients.

  7. #7
    Quote Originally Posted by sach_nyc View Post
    Just tried it but got below issues

    domain.com goes to https://domain.com but not https://www.domain.com

    https://domain.com shows certificate warning as certificate is only for www.domain.com
    What about this?

    Code:
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1
    █‏ █‏ Phil Kinsley - NotionHosting
    █‏ █‏ cPanel | SSD | CloudLinux | LiteSpeed | R1Soft Backups and more!
    █‏ █‏ Shared Hosting | Managed Hosting | Self/Fully Managed VPS | Dedicated Servers | Discounted Domains

  8. #8
    Join Date
    Jul 2014
    Posts
    42

    Did not work

    Quote Originally Posted by [HB]Andrei View Post
    This should get the job done, add it to your .htaccess file:

    Code:
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^$
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    Also if your certificate works fine on www it should work just as fine on non-www, it could just be a cache issue on your end seeing the cert error on non-www.
    Works fine except it did not move https://domain.com/ to https://www.domain.com/ so i got warning.

    Certificate will only work for www version as I have multi domain SSL which only has www.domain.com

  9. #9
    Join Date
    Jul 2014
    Posts
    42

    Works

    Quote Originally Posted by PhilK View Post
    What about this?

    Code:
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1

    This works correctly. Thanks so much

  10. #10
    Join Date
    Jul 2014
    Posts
    42
    Quote Originally Posted by sach_nyc View Post
    This works correctly. Thanks so much
    I am sorry. this did not work after testing all scenarios. It duplicates the part of url after redirection and does not redirect https://domain.com/ to https://www.domain.com/

    I am using multi domain ssl certificate.

  11. #11
    Join Date
    Jul 2014
    Posts
    42

    Thanks but

    Quote Originally Posted by [HB]Andrei View Post
    This should get the job done, add it to your .htaccess file:

    Code:
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^$
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    Also if your certificate works fine on www it should work just as fine on non-www, it could just be a cache issue on your end seeing the cert error on non-www.
    Did not notice i replied earlier. So, I edited this post

Similar Threads

  1. redirect to HTTPS
    By loodwood in forum Hosting Security and Technology
    Replies: 7
    Last Post: 09-12-2012, 08:28 AM
  2. .htaccess https redirect different than http redirect
    By MTO in forum Hosting Security and Technology
    Replies: 4
    Last Post: 05-24-2012, 05:14 AM
  3. http/https www to non-www https redirect
    By StealthyHosting in forum Web Design and Content
    Replies: 6
    Last Post: 05-23-2012, 10:26 PM
  4. Can I redirect to https: ?
    By LeMarque in forum Hosting Security and Technology
    Replies: 5
    Last Post: 10-19-2007, 03:55 PM
  5. .htaccess redirect all pages to https://www
    By LJ Host in forum Hosting Security and Technology
    Replies: 3
    Last Post: 07-17-2007, 02:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •