Apache Mod将所有传入的URL重写为https://www [英] Apache Mod Rewrite all incoming urls to https://www

查看:76
本文介绍了Apache Mod将所有传入的URL重写为https://www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将所有传入的URL重定向到我的网站,以用于规范化目的的单个URL.

应满足以下重定向条件

  1. http://example.com- > http://www.example.com -> https://example.com -> https://www.example.com
  2. www.example.com-> https://www.example.com

我目前用httpd.conf编写的重写规则如下所示

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]

使用上述规则,我能够达到第一,第二和第四条规则,但是第三条对我不起作用. 欢迎任何建议.

解决方案

您网站的HTTPS和HTTP版本的定义必须(?)出现在不同的virtualhost条目下(除非您在httpd前面使用某些代理). /p>

这很容易导致两个虚拟主机中配置之间的差异.确保重定向的配置在端口80和端口443的两个虚拟主机中都相同

最简单的方法是使用Include语句并在两个virtualhost定义中引用一个公共文件

例如,将这些重写内容放在根配置目录中的文件base-rewrite.inc中(在debian/ubuntu中为/etc/apache2/base-rewrite.inc;在Redhat中为/etc/httpd/base-rewrite.inc):

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]

然后在您的虚拟主机定义中,Include此文件:

<VirtualHost *:80>
    # HTTP default virtualhost
    ServerName www.example.com
    DocumentRoot /var/www/html/

    Include base-rewrite.inc
</VirtualHost>

<VirtualHost *:443>
    # HTTPS default virtualhost
    ServerName www.example.com
    DocumentRoot /var/www/html/

    # An example SSL config, but use your certificate files
    SSLEngine on
    SSLCertificateFile      /etc/ssl/certs/ssl-cert-snakeoil.pem
    SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key

    Include base-rewrite.inc
</VirtualHost>

这将使重写保持一致,并易于维护和一次性更改所有内容

I want to redirect all incoming urls to my website to a single url for canonicalization purposes.

Following redirect conditions should meet

  1. http://example.com-> https://www.example.com
  2. http://www.example.com -> https://www.example.com
  3. https://example.com -> https://www.example.com
  4. www.example.com -> https://www.example.com

My current Rewrite rules written in httpd.conf look as follows

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]

With the above rules I am able to achieve 1st,2nd and 4th rule but 3rd doesn't work for me. Any suggestions are welcome.

解决方案

The definition for HTTPS and HTTP version of your site must (?) appear under different virtualhost entries (unless you are using some proxy in front of httpd).

This makes it easy to end up with differences between the configs in the two virtualhosts. Make sure the config for the redirect is the same in both virtualhosts for port 80 and port 443

The easiest way to do this is to make use of an Include statement and reference a common file in the two virtualhost definitions

For instance put these rewrites in a file base-rewrite.inc in the root config dorectory (/etc/apache2/base-rewrite.inc in debian/ubuntu; /etc/httpd/base-rewrite.inc in Redhat):

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]

Then in your virtual hosts definition, Include this file:

<VirtualHost *:80>
    # HTTP default virtualhost
    ServerName www.example.com
    DocumentRoot /var/www/html/

    Include base-rewrite.inc
</VirtualHost>

<VirtualHost *:443>
    # HTTPS default virtualhost
    ServerName www.example.com
    DocumentRoot /var/www/html/

    # An example SSL config, but use your certificate files
    SSLEngine on
    SSLCertificateFile      /etc/ssl/certs/ssl-cert-snakeoil.pem
    SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key

    Include base-rewrite.inc
</VirtualHost>

This will keep the rewrites consistent and make it easy to maintain and change them all at once

这篇关于Apache Mod将所有传入的URL重写为https://www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆