【问题标题】:CodeIgniter htaccess modification for semi https and without www versionCodeIgniter htaccess 修改为半 https 且无 www 版本
【发布时间】:2014-07-22 16:07:07
【问题描述】:

而不是在核心 PHP 中,htaccess 的修改是不同的。 所以,经过一番搜索,我得到了以下代码。

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

  • 严禁在整个站点中使用 www。即直接重定向到非 www 版本。
  • HTTPS 适用于某些页面。 (结帐,登录页面)。其他页面严格使用 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] 

在这个新的更新代码上,htaccess 测试器站点完美地显示了所有内容。 但是,更新到网站后,有很多问题。

当我打开这个链接

https://example.com/login

它一直在循环,而不是停止。 在这个链接上 http://www.example.com/login 它重定向到

https://login/login

【问题讨论】:

  • 试试RewriteRule ^(.*)$ http://%1/$1 [R=301,L,QSA]
  • 兄弟你能详细说明一下,我在哪里可以添加这一行,或者我应该用什么替换,因为你给的建议我以前试过但没有奏效。这就像我缺少一些行或缺少一些排序。
  • 而不是RewriteRule ^(.*)$ http://%1%{REQUEST_URI}/$1 [R=301,NE,L]
  • 不。它不会从链接中删除 www。
  • 您的 .htaccess 在哪里 - 在某个文件夹或网站的根目录中?如果在根目录中,则问题出在其他地方,因为我编写的规则有效。你可以在这里试试,例如htaccess.madewithlove.be为重写规则启用日志记录并查看日志。

标签: php apache .htaccess codeigniter


【解决方案1】:

单独处理 no-www 和 toggle-protocol 重定向更容易。在以下示例中:

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

【讨论】:

  • 非常感谢您几乎正确的回复。但我仍然在两种情况下面临这个问题。 ► https://www.example.com 指向相同的 url 而它应该只指向 http://example.comhttps://example.com 指向相同的 url 而它应该只指向 http://example.com 。通常,https 案例会产生问题而不是重定向。
  • Force NOWWW 规则应将 www 重定向到 no-www,而 Force HTTP 规则应将 https 重定向到 http。您能否仔细检查 (i) HTTPS 网站使用相同的 htaccess 文件 (ii) 没有浏览器缓存问题(将随机查询字符串附加到 URL 或空浏览器缓存进行测试)。
猜你喜欢
  • 2021-03-25
  • 1970-01-01
  • 2014-12-16
  • 1970-01-01
  • 2019-10-05
  • 1970-01-01
  • 2018-07-03
  • 1970-01-01
  • 2019-12-04
相关资源
最近更新 更多