Http https htaccess redirect error
I have a site that I'm trying to completely redirect from http
to https
.
I need to redirect any user who comes to the site via http
to the same page in https
.
The site is hosted by the provider's shared hosting. Since the domain's SSL certificate has been installed, a new IP address has been assigned to the new domain.
I was advised that the following code should be added to my file .htaccess
. There are also some rules that handle SEO friendly URLs.
RewriteEngine On
RewriteBase /
# enforce https and www
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,NE,R=302]
# skip further rules for files and directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# internal rewrite rules
RewriteRule ^([\w-]+)/([\w-]+)/?$ page.php?venue=$1&artist=$2 [L,QSA]
RewriteRule ^([\w-]+)/?$ page.php?venue=$1 [L,QSA]
For some reason, adding this code to the file .htaccess
results in a redirect loop error. I've tried line variants RewriteCond %{SERVER_PORT} !=443
as recommended for other stack overflow posts, but nothing seems to work.
Anyone have a solution?
source to share
I am provided with this code from my service provider which seems to do the trick using php
<?php
if ($_SERVER['HTTP_X_FORWARDED_SSL'] == '1') { } else { $redirect = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; header("Location: $redirect"); } ?>
I can add the following for permanent redirection
header("HTTP/1.1 301 Moved Permanently");
source to share
This code can be used in .htaccess root directory:
RewriteEngine On
RewriteBase /
# enforce https and www
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,NE,R=302]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]
# skip further rules for files and directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# internal rewrite rules
RewriteRule ^([\w-]+)/([\w-]+)/?$ page.php?venue=$1&artist=$2 [L,QSA]
RewriteRule ^([\w-]+)/?$ page.php?venue=$1 [L,QSA]
source to share