Home .htaccess redirect HTTP to HTTPS with mod_rewrite not working
Reply: 1

.htaccess redirect HTTP to HTTPS with mod_rewrite not working

Jordi72
1#
Jordi72 Published in 2018-02-14 11:26:01Z

I'm trying to redirect all the connections to my website from http to https using htaccess and mod_rewrite, I've tried all the possible things and still not working, I get an error like: The page is not redirecting properly

Here goes the htaccess code:

ErrorDocument 404 http://www.example.com/error.php

<IfModule mod_rewrite.c>

Options -Indexes

Options +FollowSymlinks

RewriteEngine on

RewriteBase /

RewriteCond %{HTTPS} on 

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} !^www\. [NC]

RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteRule ^([^/]*)\.html$ /index.php?lang=$1 [L]

RewriteRule ^sites/examplesite/([^/]*)\.html$ /index.php?lang=$1 [L]

RewriteRule ^([^/]*)/examplesite/([^/]*)\.html$ /index.php?lang=$1&page=$2 [L]

RewriteRule ^([^/]*)/([^/]*)\.html$ /index.php?lang=$1&page=$2 [L]

RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?lang=$1&idCat=$2&page=$3 [L]

RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?lang=$1&cat=$2&filter=$3&pa=$4&page=$5 [L]

RewriteRule ^sites/examplesite/([^/]*)/([^/]*)\.html$ /index.php?lang=$1&page=$2 [L]

RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?lang=$1&show=$2&page=$3 [L]

RewriteRule ^sites/examplesite/([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?lang=$1&itemID=$2&idCat=$3&page=$4 [L]

RewriteRule ^sites/examplesite/([^/]*)//([^/]*)/([^/]*)\.html$ /index.php?lang=$1&idCat=$2&page=$3 [L]

RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?lang=$1&idCat=$2&client_id=$3&page=$4 [L]

</IfModule>

<FilesMatch "\.(ttf|otf|eot|woff)$">
  <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
  </IfModule>
</FilesMatch>



   php_flag display_errors On
Mohammed Elhag
2#
Mohammed Elhag Reply to 2018-02-14 13:15:16Z

Regarding your main issue , from your code , this part i is what you think that will do forcing all http requests to https:

RewriteCond %{HTTPS} on 

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} !^www\. [NC]

RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

and it is clear that you also want to add www for none www , so first , when you want to catch none https request , your should put like this line :

RewriteCond %{HTTPS} off 

because it is condition like saying , if request not into https but in your code you only catch https requests themselves and force them again.

Put the following code instead of the above:

 RewriteCond %{HTTPS} off [OR]
 RewriteCond %{HTTP_HOST} !^www\. [NC]
 RewriteRule ^(.*)$ https://www.yourwebsite.com/$1 [R=302,L]

Test it and if it's ok change 302 with 301 to get permanent redirection

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 2.678029 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO