CodeIgniter htaccess修改半https和没有www版本

而不是在核心PHP中,htaccess的修改是不同的。 所以,一些search后,我已经达到了以下代码。

在此之前,我的要求是我的项目的标准。

  • www是严格禁止在整个网站。 即直接redirect到非www版本。
  • HTTPS适用于某些页面。 (结帐,login页面)。 严格的HTTP为其他网页。
  • 删除CodeIgniter的默认index.php

这是我的代码。

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] 

问题:无法达到非www版本网站的结果


更新(10月17日)

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

在这个新的更新的代码,htaccesstesting仪网站显示一切完美。 但是,更新到网站后有很多问题。

当我打开这个链接

 https://example.com/login 

它保持循环,而不是停止。 并在此链接http://www.example.com/loginredirect到

 https://login/login 

分别处理no-www和toggle-protocol重定向比较容易。 在以下示例中:

  1. www请求按原样重定向到no-www(在下一个请求中处理切换协议重定向)
  2. 所选页面的http请求被重定向到https版本
  3. 其他页面的https请求被重定向到http版本

请注意以下示例中的前导/...(/|$) 。 他们确保完整的路径片段匹配(例如在第三组规则/jsmith/profile /js/main.js /jsmith/profile /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 

注意 :请记住在测试后用301代替302。