.htaccess - 如何使用 htaccess 文件将 http 重定向到 https://www.example.com?
问题描述
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
我已经使用Codeigniter
框架创建了一个网站,并且我的域和托管在GoDaddy
. 现在,我已经安装SSL certificate
并管理了我的网站。现在,当我example.com
在 URL 中使用它时,它会重定向我,https://www.example.com
但是当我点击我的login
页面时,它会显示我Not Found
并且 URL 看起来像https://www.example.com/index.php?/login
,但我想要 URL Like https://www.example.com/login
。那么,我该怎么做呢?请帮忙。
谢谢你
解决方案
- 更改规则的顺序
- 有一个规则来重定向
www
和http -> https
你的 .htaccess 应该是这样的:
RewriteEngine on
# redirect for adding www and https
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
确保使用新的浏览器进行测试。
推荐阅读
- octobercms - 博客分页不起作用
- java - 带有可能的空指针的骆驼日志消息
- dependency-injection - play 2.6.x 环境依赖注入
- android - 如何以 xamarin 形式使用系统时钟/时间?
- java - 春季启动火花应用
- python - Google Pubsub 推送到 AppEngine 502 和 504 错误
- javascript - 代理数组有一个与方法名同名的键
- amazon-web-services - 端口 80 和 443 关闭
- shell - 使用 awk 在输出中添加自定义列
- python - 尝试将 json 文件加载到扁平化的 pandas DataFrame 中