CodeIgniter htaccess for semi https and without www version

Instead of basic PHP, changing htaccess is different. So, after some searching, I got to the following code.

Prior to this, my requirements are standard in my project.

  • www is strictly prohibited throughout the site. that is, redirect directly to a version other than www.
  • HTTPS for some pages. (verification, login page). Strictly HTTP for other pages.
  • removing the CodeIgniter default.php index

Here is my code.

RewriteEngine on

#################################
# force HTTPS
RewriteCond %{HTTPS} =off
RewriteCond %{REQUEST_URI} (checkout|login)
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# force HTTP
RewriteCond %{HTTPS} =on
#RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{REQUEST_URI} !(checkout|login|css|img|js)
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Strict to non-www version >>> Not Working
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI}/$1 [R=301,NE,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [L] 

Problem: Unable to achieve non-www version of site


update (October 17th)

# Strict to non-www version.
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{REQUEST_URI} (login|account_detail|alternate_address|update_password)
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,QSA]

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{REQUEST_URI} !(login|account_detail|alternate_address|update_password|https|css|img|js|resources|images)
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,QSA]

# force HTTPS
RewriteCond %{HTTPS} =off
RewriteCond %{REQUEST_URI} (login|account_detail|alternate_address|update_password)
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# force HTTP
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !(login|account_detail|alternate_address|update_password|https|css|img|js|resources|images)
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [L] 

In this new updated code, the htaccess tester website displays everything perfectly. But after updating the site there are many problems.

When I open this link

https://example.com/login

, . http://www.example.com/login

https://login/login
+4
1

no-www toggle-protocol . :

  • www no-www as-is ( )
  • HTTP- https
  • https http-

/...(/|$) . (, /js/main.js, /jsmith/profile ).

RewriteEngine On

# Force NOWWW
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]

# Force HTTPS
RewriteCond %{HTTPS} =off
RewriteCond %{REQUEST_URI} /(login|account_detail|alternate_address|update_password)(/|$)
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]

# Force HTTP
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !/(login|account_detail|alternate_address|update_password|css|img|js)(/|$)
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]

# Hijack all requests
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [L]

:

http://www.example.com/
    http://example.com/

http://www.example.com/a/b?c=3&d=4
    http://example.com/a/b?c=3&d=4

http://www.example.com/login
    http://example.com/login
        https://example.com/login

https://www.example.com/js/javascript.js
    https://example.com/js/javascript.js
        http://example.com/js/javascript.js

. 302 301 .

+3

Source: https://habr.com/ru/post/1542928/


All Articles