【问题标题】:apache rewrite: force https and name of server, not IPapache重写:强制https和服务器名称,而不是IP
【发布时间】:2011-05-22 08:39:01
【问题描述】:

我希望我的 apache 始终强制人们使用 https 并将基于 IP 的查找映射以转发到服务器名称。以下设置(在 httpd.conf 文件中)负责 http 到 https 的重定向:

<Location />
    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://my_server.example.com%{REQUEST_URI}
</Location>

但现在我还希望,如果人们键入 192.168.1.2,他们会被重定向到 my_server.example.com。 总结一下:

http://192.168.1.2/someurl -> https://my_server.example.com/someurl

我尝试了几件事,但要么我的设置被忽略,要么我最终进入重定向循环。 有什么提示吗?

【问题讨论】:

    标签: apache mod-rewrite redirect


    【解决方案1】:

    如果您可以访问主配置,而不仅仅是 .htaccess,那么使用单独的虚拟主机最容易做到这一点,而不是求助于瑞士军队的电锯 mod_rewrite。

    <VirtualHost *:80>
        Redirect permanent / https://my_server.example.com/
    </VirtualHost>
    <VirtualHost *:443>
        Redirect permanent / https://my_server.example.com/
        SSLEngine on
    </VirtualHost>
    
    <VirtualHost *:443>
        ServerName my_server.example.com
        SSLEngine on
        ...real site config...
    </VirtualHost>
    

    您通常希望重定向到规范主机名的不仅仅是数字 IP 地址访问,而是您控制的已知良好域以外的所有地址。在配置中首先放置一个默认虚拟主机,它会重定向(或不提供任何服务)有助于避免某些基于 DNS 的 XSS 攻击。

    【讨论】:

      【解决方案2】:

      我想出了以下解决方案:

      <Location />
          Options +FollowSymLinks
          RewriteEngine On
          RewriteCond %{HTTP_HOST} !^my_server\.example\.com [NC]
          RewriteRule (.*) https://my_server.example.com%{REQUEST_URI} [R=301,L]
          RewriteCond %{HTTPS} off
          RewriteRule (.*) https://my_server.example.com%{REQUEST_URI}
      </Location>
      

      它完全符合我的需要。

      【讨论】:

        猜你喜欢
        • 2015-04-15
        • 2021-12-05
        • 2021-04-14
        • 2020-07-03
        • 2013-01-03
        • 2020-12-01
        • 2018-08-25
        • 1970-01-01
        • 2019-01-09
        相关资源
        最近更新 更多