我正在使用codeigniter框架,设置。htaccess文件以删除index.php并试图为服务器启用HTTPS协议,然后发生了一些事情。
HTTP:
- 一切都很好,当我访问http://www.example.com/controller/method或http://www.example.com/index.php/controller/method
HTTPS:
-
一切正常,当我访问https://www.example.com/index.php/controller/method
-
当我访问https://www.example.com/controller/method时得到404没有找到
我认为这是。htaccess文件的问题,它看起来像htaccess文件不工作的HTTPS协议。
。我的网站的Htaccess文件
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond $1 !^(index'.php|(.*)'.swf|forums|images|css|downloads|js|robots'.txt|favicon'.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
有什么问题吗?非常感谢。
注意:SSL重写应该发生在.htaccess
文件的index.php
重写之前。
所以,不要做这个设置(因为我们不改变服务器apache配置文件中的任何东西)。)
AllowOverride所有的
只需应用设置2就可以了。
我遇到过同样的问题,通过添加
解决了这个问题AllowOverride所有的
设置1:我们有两个配置文件。
- Apache默认配置(使用http运行)
- SSL配置(与https一起运行)
进入SSL配置并添加
<Directory "/var/www/html">
AllowOverride All
</Directory>
**设置2:** .htaccess文件应该是这样的…
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
首先从配置文件中删除$config['base_url],并在$config['index']属性中添加" (blank) .
请将。htaccess文件设置为…
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
试试这个
我在我的一个项目中使用了这个。愿它对你有所帮助
#AuthType Basic
#AuthName "restricted area"
#AuthUserFile /home/site_name/public_html/.htpasswd
#require valid-user
RewriteEngine On
#RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.site_name.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?.site_name.lk$ [NC]
RewriteRule ^(.*)$ https://www.site_name.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?site_name'.com$ [NC]
RewriteCond %{HTTP_HOST} !^www'. [NC]
RewriteRule .? https://www.%1site_name.com%{REQUEST_URI} [R=301,L]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond $1 !^(index'.php|assets|files|robots'.txt)
# RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
注意上面的
site_name
应该替换为你的网站名称
你可以试试这个,它适用于我的项目。
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond $1 !^(index'.php|(.*)'.swf|forums|images|css|downloads|js|robots'.txt|favicon'.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
参考:https://wiki.apache.org/httpd/RewriteHTTPToHTTPS
如果你想要http->https和删除www和删除index.php这是解决方案:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www'. [NC]
RewriteCond %{HTTP_HOST} ^(?:www'.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>