CodeIgniter htaccess modification for semi https a

2019-05-06 02:20发布

问题:

Rather than in core PHP, htaccess modification is different. So, after some searching I have reached to the following code.

before that, my requirements are standard in my project.

  • www is strictly not allowed in whole site. i.e. redirect directly to non-www version.
  • HTTPS is for some pages. (checkout, login page) . Strictly HTTP for other pages.
  • removing CodeIgniter's default index.php

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] 

Issue: Not able to reach to result of non-www version of site


update(17 Oct)

# 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] 

On this new updated code, htaccess tester site displaying everything perfectly. But, after updating to site there are many issues.

When I open this link

https://example.com/login

It keeps looping, not stopping. And on this link http://www.example.com/login it redirects to

https://login/login

回答1:

It is easier to handle the no-www and toggle-protocol redirects separately. In the following example:

  1. The www requests are redirected to no-www as-is (handle toggle-protocol redirect on next request)
  2. The http requests for selected pages are redirected to https version
  3. The https requests for other pages are redirected to http version

Note the leading /...(/|$) in the following examples. They ensure that complete path fragments are matched (e.g. in the third set of rules /js/main.js matches but /jsmith/profile does not).

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]

The following tests passed:

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

Note: Remember to replace 302 with 301 after testing.