http到https apache重定向

时间:2013-04-24 19:23:53

标签: linux apache webserver

环境带有apache的Centos

尝试设置从http到https

的自动重定向
From manage.mydomain.com --- To ---> https://manage.mydomain.com 

我已经尝试将以下内容添加到我的httpd.conf中,但它无法正常工作

 RewriteEngine on
    ReWriteCond %{SERVER_PORT} !^443$
    RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]

有什么想法吗?

13 个答案:

答案 0 :(得分:191)

我实际上已经按照这个例子,它对我有用:)

NameVirtualHost *:80
<VirtualHost *:80>
   ServerName mysite.example.com
   DocumentRoot /usr/local/apache2/htdocs 
   Redirect permanent / https://mysite.example.com/
</VirtualHost>

<VirtualHost _default_:443>
   ServerName mysite.example.com
  DocumentRoot /usr/local/apache2/htdocs
  SSLEngine On
 # etc...
</VirtualHost>

然后做:

/etc/init.d/httpd restart

答案 1 :(得分:109)

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}

http://www.sslshopper.com/apache-redirect-http-to-https.html

http://www.cyberciti.biz/tips/howto-apache-force-https-secure-connections.html

答案 2 :(得分:83)

搜索apache redirect http to https并在此处登陆。这就是我在ubuntu上所做的:

1)启用模块

sudo a2enmod rewrite
sudo a2enmod ssl

2)编辑您的站点配置

编辑文件

/etc/apache2/sites-available/000-default.conf

内容应该是:

<VirtualHost *:80>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>

<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile    <path to your crt file>
    SSLCertificateKeyFile   <path to your private key file>

    # Rest of your site config
    # ...
</VirtualHost>

3)重启apache2

sudo service apache2 restart

答案 3 :(得分:11)

实际上,您的主题属于https://serverfault.com/,但您仍可以尝试检查这些 .htaccess 指令:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}/$1

答案 4 :(得分:8)

使用mod_rewrite不是推荐的方式,而是使用虚拟主机和重定向。

如果您倾向于使用mod_rewrite:

RewriteEngine On
# This will enable the Rewrite capabilities

RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS

RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same 
location but using HTTPS.
# i.e.  http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in
# httpd.conf or .htaccess context

参考:Httpd Wiki - RewriteHTTPToHTTPS

如果您正在寻找301永久重定向,则重定向标志应为as,

 R=301

所以RewriteRule就像,

RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]

答案 5 :(得分:4)

如果你有Apache2.4,请检查000-default.conf - 删除DocumentRoot并添加

Redirect permanent / https://[your-domain]/

答案 6 :(得分:4)

服务器版本:Apache / 2.4.29(Ubuntu)

经过长时间的网络搜索和apache的官方文档,唯一适用于我的解决方案来自 /usr/share/doc/apache2/README.Debian.gz

Election *Election2018 = new Election();
Committee com1 = new Committee();
com1.RegVoter(Election2018->getRegLogBook());

在文件/etc/apache2/sites-available/000-default.conf中添加

  

重定向“ /”“ https://sub.domain.com/

const GoogleSpreadsheet = require('google-spreadsheet');
const creds = require('./client_secret.json');
var request = require('request');

const SPREADSHEET_ID = '1_12Wz7865j8f3ktPtWyubnsXNSHAsUD0DBGSR2z0pZk';

// Create a document object using the ID of the spreadsheet - obtained from its URL.
let doc = new GoogleSpreadsheet(SPREADSHEET_ID);

// Authenticate with the Google Spreadsheets API.
doc.useServiceAccountAuth(creds, function (err) {

  // Get all of the rows from the spreadsheet.
  doc.getRows(1, function (err, rows) {
    if(err) {
      console.log(err);
    }
    // console.log(`Loaded doc: ` + rows.title + ` by ` + rows.author.email)
    console.log("!!!! Loaded doc!!");

    const sheet = rows[0];
    // console.log("=================" + 
    //   `sheet 1: ` + sheet.title + ` ` + sheet.rowCount + `x` + sheet.colCount
    // )

    for (let i = 0; i < rows.length; i++) {

      console.log("=================" + 
        `sheet ${i}: ` + rows[i]['Tasks View Id'], rows[i]['Tasks View Index'], rows[i]['Tasks Task Name']
      )
    }

  });

  // Add a new row 
  doc.addRow(1, { Tasks_View_Id: 'ROse', age: 20, class: "Dance" }, function(err) {
    if(err) {
      console.log(err);
    }
    console.log("+++++++++++++++ Insert Succeed!");
  });

// Post request
  request.post(
    'http://fusioncode2.crane.ai:2233/',
    { json: { key: 'value' } },
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body)
        }

    }

);

});

就是这样。


P.S:如果您想阅读本手册而无需提取以下内容:

To enable SSL, type (as user root):

    a2ensite default-ssl
    a2enmod ssl

答案 7 :(得分:3)

这对我有用:

RewriteCond %{HTTPS} =off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,L,R=301]

答案 8 :(得分:2)

此代码适用于我。

# ----------port 80----------
RewriteEngine on
# redirect http non-www to https www
RewriteCond %{HTTPS} off
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

# redirect http www to https www
RewriteCond %{HTTPS} off
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

# ----------port 443----------
RewriteEngine on
# redirect https non-www to https www
RewriteCond %{SERVER_NAME} !^www\.(.*)$ [NC]
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

答案 9 :(得分:1)

请在apache Virtualhosting配置中尝试此操作 然后重新加载apache服务

RewriteEngine On

RewriteCond %{HTTPS} off


RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}

答案 10 :(得分:0)

对我来说这有效

RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

答案 11 :(得分:-1)

我需要这样做,就像将所有http流量从服务器上默认的Apache主页重定向到https上提供的流量一样。

由于在配置apache时我还是很绿色,所以我宁愿避免直接使用mod_rewrite而是选择更简单的方法:

<VirtualHost *:80>
  <Location "/">
     Redirect permanent "https://%{HTTP_HOST}%{REQUEST_URI}"
  </Location>
</VirtualHost>

<VirtualHost *:443>
  DocumentRoot "/var/www/html"
  SSLEngine on
  ...
</VirtualHost>

我之所以喜欢它,是因为它允许我使用apache变量,因此不必指定实际的主机名,因为它只是一个没有关联域名的IP地址。

参考: https://stackoverflow.com/a/40291044/2089675

答案 12 :(得分:-1)

请注意-执行此操作时,您将丢失所有 Facebook喜欢(前提是您开始通过 //Getting the column from the colors for all the columns public static void getColors(Document doc) { String expressionGroupedCol = "/DynamicReport/Columns/Column/FormattingInfo/Header/FontInfo"; XPath xPath = XPathFactory.newInstance().newXPath(); try { NodeList nodeList = (NodeList) xPath.compile(expressionGroupedCol).evaluate( doc, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); i++) { Node nNode = nodeList.item(i); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; String strTextColor = eElement.getElementsByTagName("TextColor").item(0).getTextContent(); System.out.println(strTextColor); String strBackgroundColor = eElement.getElementsByTagName("BackgroundColor").item(0).getTextContent(); System.out.println(strBackgroundColor); } } } catch (Exception e) { e.printStackTrace(); } } 连接收集喜欢的对象!

改为使用JavaScript

http

并将其放在标题的最后。