apache redrict 301从https://www.domain.com到https://domain.com

时间:2013-08-18 00:00:59

标签: apache http ssl https rewrite

我试着花几个小时来重新审视https:// www。 domain .com到https:// domain .com with 301。

我有域名.com的证书但没有www的通配符。 domain .com。

我尝试了以下内容:

RewriteEngine On
RewriteCond        %{HTTPS}        =on
RewriteCond        %{HTTP_HOST}    ^www\.meinedomain\.de$ [NC]
RewriteRule        ^(.*)            https://meinedomain.de/$1 [L,R=301]

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Redirect 301 / https://mineyourmind.de/

这些都不起作用。什么想法可能是错的?我已经将它直接添加到apache设置中,它对于http:// www非常有用。 domain .com和http:// domain .com到https:// domain .com但不适用于https:// www。 domain .com。


目前我使用以下配置。每个redrict都可以正常工作,而不是https://www.mineyourmind.dehttps://mineyourmind.de

这样的文件怎么样?

<VirtualHost *:80>
    ServerName mineyourmind.de
    ServerAlias www.mineyourmind.de
    Redirect 301 / https://mineyourmind.de/ 
</VirtualHost>

<VirtualHost *:443>
    ServerAdmin **
    DocumentRoot /var/www/mineyourmind
    ServerName mineyourmind.de
    ServerAlias www.mineyourmind.de
    SSLEngine on
    SSLCertificateFile    /etc/**
    SSLCertificateKeyFile /etc/**
    SSLCertificateChainFile /etc/**
    SSLCACertificateFile /etc/**

    RewriteEngine On
    RewriteCond %{HTTPS} on
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    <Directory />
        Options FollowSymLinks
        AllowOverride None
        Order deny,allow
        deny from all
    </Directory>
    <Directory /var/www/mineyourmind/>
        Options -Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

第二次编辑:

<VirtualHost *:80>
    ServerName mineyourmind.de
    ServerAlias www.mineyourmind.de
    RewriteEngine On
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\.mineyourmind\.de$ [NC]
    RewriteRule (.*) https://mineyourmind.de%{REQUEST_URI} [R=301,L]
</VirtualHost>

<VirtualHost *:443>
    ServerAdmin **
    DocumentRoot /var/www/mineyourmind
    ServerName mineyourmind.de
    ServerAlias www.mineyourmind.de
    SSLEngine on
    SSLCertificateFile    /etc/apache2/**
    SSLCertificateKeyFile /etc/apache2/ssl/**
    SSLCertificateChainFile /etc/apache2/ssl/**
    SSLCACertificateFile /etc/apache2/ssl/**
    RewriteEngine On
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\.mineyourmind\.de$ [NC]
    RewriteRule (.*) https://mineyourmind.de%{REQUEST_URI} [R=301,L]
    <Directory />
        Options FollowSymLinks
        AllowOverride None
        Order deny,allow
        deny from all
    </Directory>
    <Directory /var/www/mineyourmind/>
        Options -Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

1 个答案:

答案 0 :(得分:0)

如果您请求https://www.meinedomain.de/并且您的证书未经www.meinedomain.de授权,则无论如何重定向,您都会收到的安全警告。重定向只能在接受安全异常后才会发生。

确保您已将这些规则放在 SSL vhost的配置中,而不是非SSL配置中。 Apache通常将它们分开。如果仍然无效,您可以尝试将其放在文档根目录中的htaccess文件中:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.meinedomain\.de$ [NC]
RewriteRule (.*) https://meinedomain.de%{REQUEST_URI} [R=301,L]