1

I tried to use .htaccess file to redirect http:// to https://
I also tried to append www. in front of domain-name, if it is not written.
I used this code in the .htaccess file

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
Options -Indexes

# Force use https for secure connections
# (as it appears on your SSL certificate)
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# Force use www in front of domain-name
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

In Chrome and IE the site opens as expected
Jumps always to https://www.domain.com, no matter if the input address is "www.domain.com" or "domain.com" But in Firefox it works correctly only when the address is "domain.com"
When I place "www.domain.com", the Firefox browser turns it into
https://www.www.domain.com
I am sorry, but I simply cannot find the logic, why Chrome and IE interpret the .htaccess so well, and Firefox does it half way. Firefox is 41.0.2, the cache is empty, no change. I am out of guesses, could anyone suggest a solution?

4
  • 1
    I would use server port not 443... e.g. 'RewriteCond %{SERVER_PORT} !^443$'. That's always worked for me in all browsers.
    – Tyson
    Commented Oct 22, 2015 at 18:51
  • 1
    Oops... Missing half: RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
    – Tyson
    Commented Oct 22, 2015 at 18:54
  • Your idea seems to have worked well. There were small typos, but I cleared them. Here;s the code I used: RewriteCond %{SERVER_PORT} !^443$ RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [R=301,L] Commented Oct 22, 2015 at 20:15
  • @Tyson Have you found the reasoning behind this port behavior?
    – Rodrigo
    Commented Mar 30, 2021 at 2:59

1 Answer 1

1

Thanks to Tyson now I have well working .htaccess file
Here is the code that worked. Change domain.com with your domain-name.

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
Options -Indexes

# Always use www in the domain
RewriteCond %{HTTP_HOST} ^([a-z.]+)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%1domain.com%{REQUEST_URI} [R=301,L]

# Check if HTTPS is not used, then jump to HTTPS
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.domain.com/$1 [NC,R=301,L]

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .