从http迁移到https的网站现在已损坏

时间:2019-12-10 16:53:02

标签: apache .htaccess codeigniter http https

我正在实习,他们要求我修复一个网站,该网站从http迁移到https后,该网站已损坏。 我变得发疯了,已经好几天了,无法修复它...我不知道该怎么办,要求我修复它的系统管理员也不知道。他们没有问题地进行了其他迁移。

我将代码中的网址从http更改为https。

这是他们执行的.htaccess:

Options -Indexes
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,

RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]

#When your application folder isn't in the system folder

RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

  #Checks to see if the user is attempting to access a valid file,
  #such as an image or css document, if this isn't true it sends the
  #request to index.php

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
  ErrorDocument 404 /index.php
</IfModule>

这是/etc/apache2/sites-available/webpagename.conf中的内容

Options -Indexes
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,

RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]

#When your application folder isn't in the system folder

RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php

RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
 ErrorDocument 404 /index.php
</IfModule>

在/ etc / apache2 / sites-available / webpagename-le-ssl,conf

他们告诉我,他们是这样迁移的: Linux命令行:

  1. certbot -apache
  2. /etc/apache2/sites-available/webpagename.conf并写下这些行。
  3. a2ensite webpagename.conf
  4. systemctl重新加载apache2
  5. journalctl -xe

有什么想法吗?您需要知道的其他任何信息才能检查问题吗?该网页是PHP / Codeigniter。

1 个答案:

答案 0 :(得分:0)

假设您正在使用CodeIgniter3(如此处How to force ssl in codeigniter?所示,并遵循文档https://codeigniter.com/user_guide/general/urls.html

您需要通过添加到config / config.php文件来启用挂钩:

$ sphinx-quickstart Welcome to the Sphinx 2.3.0 quickstart utility.

然后在config文件夹(即application / config / hooks.php)中创建一个名为hooks.php的新文件,并在其中添加以下代码:

$config['enable_hooks'] = TRUE;

现在在应用程序文件夹(即application / hooks)中创建一个名为hooks的新目录,然后在hooks文件夹(即application / hooks / ssl.php)中创建一个名为ssl.php的新文件。

在ssl.php文件中添加以下代码:

$hook['post_controller_constructor'][] = array(
    'function' => 'redirect_ssl',
    'filename' => 'ssl.php',
    'filepath' => 'hooks'
);

此外,您可以在config / config.php中将基本URL设置为https:

function redirect_ssl() { $CI =& get_instance(); $class = $CI->router->fetch_class(); $exclude = array(); // add more controller name to exclude ssl. if(!in_array($class,$exclude)) { // redirecting to ssl. $CI->config->config['base_url'] = str_replace('http://', 'https://', $CI->config->config['base_url']); if ($_SERVER['SERVER_PORT'] != 443) redirect($CI->uri->uri_string()); } else { // redirecting with no ssl. $CI->config->config['base_url'] = str_replace('https://', 'http://', $CI->config->config['base_url']); if ($_SERVER['SERVER_PORT'] == 443) redirect($CI->uri->uri_string()); } }

最后,在根文件夹中设置.htaccess文件:

$config['base_url'] = 'https://www.my-site.com/';

现在,您可以在.htaccess中逐一添加每个规则,以检查迁移情况。