2011-05-22 90 views
1

我希望我的apache始终强制人们使用https并将基于IP的查找映射为转发到服务器名称。下面的设置(httpd.conf文件)照顾HTTP到HTTPS重定向:Apache重写:强制https和服务器名称,而不是IP

<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 

我已经试过几件事情,但无论是我的设置都被忽略,或者我在一个重定向循环结束了。 任何提示?

回答

1

我想通了以下解决方案:

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

它不正是我需要的。

2

如果你有权访问主配置,而不仅仅是.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攻击。