环境Centos与apache

试图设置自动重定向从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]

什么好主意吗?


当前回答

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

在网上和apache的官方文档中搜索了很长时间后,唯一适合我的解决方案来自/usr/share/doc/apache2/README.Debian.gz

To enable SSL, type (as user root):

    a2ensite default-ssl
    a2enmod ssl

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

重定向"/" "https://sub.domain.com/"

<VirtualHost *:80>

    #ServerName www.example.com
    DocumentRoot /var/www/owncloud
    Redirect "/" "https://sub.domain.com/"

就是这样。


注:如果你想阅读手册而不提取:

gunzip -cd /usr/share/doc/apache2/README.Debian.gz

其他回答

这招对我很管用:

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

搜索apache重定向http到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>

注意,SSL模块需要证书。您需要指定一个现有的证书(如果您已经购买了),或者自己生成一个自签名证书。

3)重新启动apache2

sudo service apache2 restart

对我来说这很有效

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

这个代码为我工作。

# ----------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]

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

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