I have the below from my .htaccess file. I am trying to get my website to redirect to https://www .Now I am not really confident/have a lot of knowledge with Apache so I am stuck. I have tried various different ways from this website to do this but I keep getting the same error of ‘redirected too many times’. That is why I thought I’d create my own question.
The website is hosted on 123-reg if that makes any difference.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#remove / at the end of URL
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{DOCUMENT_ROOT}/$1/index.html -f
RewriteRule ^(.+?)/?$ /$1/index.html [L]
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
<ifModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/xml text/css text/plain
AddOutputFilterByType DEFLATE image/svg+xml application/xhtml+xml application/xml
AddOutputFilterByType DEFLATE application/rdf+xml application/rss+xml application/atom+xml
AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript application/json
AddOutputFilterByType DEFLATE application/x-font-ttf application/x-font-otf
AddOutputFilterByType DEFLATE font/truetype font/opentype
</ifModule>
<ifmodule mod_expires.c>
<Filesmatch ".(jpg|jpeg|png|gif|js|css|swf|ico|woff|mp3)$">
ExpiresActive on
ExpiresDefault "access plus 1 year"
</Filesmatch>
</ifmodule>
<ifModule mod_headers.c>
Header unset ETag
FileETag None
<FilesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css)$">
Header unset Last-Modified
</FilesMatch>
</ifModule>
<IfModule mod_headers.c>
<FilesMatch ".(js|css|xml|gz)$">
Header append Vary: Accept-Encoding
</FilesMatch>
</IfModule>
2
Answers
After a lot of trial and error. I have finally fixed this by using:
Looks like
{ENV:HTTPS}
did the trick for me.With Apache, I usually find it easier to redirect to https on my virtual hosts
on the terminal
sudo nano /etc/apache2/sites-available/000-default.conf
(march it to the location of your apache2 files)Then add the redirect rules at the end of your vhost
so that your vhost looks something like this