2011-05-22 91 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攻擊。