htaccess – redirect非https并为每个子域设置不同的默认页面

我的域名有一个通配符ssl。 我有许多子域,每个都有自己的默认页面。

www.domain.com/main.html [/home/domain/main.html]
try.domain.com/free.php [/home/domain/try/free.php]
new.domain.com/signup.php [/home/domain/new/signup.php]
等等

我想写.htaccess这样的:

  1. 所有stream量都被定向到https
  2. 子域名parsing为相应的默认页面
  3. 除了保留subdomain.domain.com的子域外,所有非www都被定向到www

我想要在单个htaccess /home/domain/.htacces中完成所有这些操作,而不是在每个文件夹中都有htaccess文件。

恕我直言,你已经走向错误的方向,你打算创build一个.htaccess文件,这是我的宠物peeve,引自.htaccess文件手册 :

如果您有权访问httpd主服务器configuration文件, 应该完全避免使用.htaccess文件。 使用.htaccess文件会降低您的Apache HTTP服务器速度 。 您可以包含在.htaccess文件中的任何指令都可以在主Apacheconfiguration文件的Directory块中更好地设置,因为它具有相同的效果,性能更好。

在端口80上设置一个默认的/ catchall VirtualHost,以便将HTTPredirect到您的安全主机:

 <VirtualHost :80> Servername www.example.com ServerAlias *.example.com # https://wiki.apache.org/httpd/RewriteHTTPToHTTPS RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^/?(.*) https://%{HTTP_HOST}/$1 [R,L] >other optional directives< </VirtualHost> 

以上将redirect到SSL时保留主机名。

那么更有效的configuration就是使用通配符SSL证书(甚至不需要依赖SNI),并为每个子域创build一个名称虚拟主机,最后一个捕获所有任何未明确定义的子域名将redirect到www

 <VirtualHost *:443> SSLEngine on >>other optional and required (SSL) directives<< SSLCertificateFile /etc/ssl/star.example.com.crt SSLCertificateKeyFile /etc/ssl/star.example.com.key ServerName "one.example.com" DocumentRoot "/var/www/html/one" </virtualHost> <VirtualHost *:443> SSLEngine on >>other optional and required (SSL) directives<< SSLCertificateFile /etc/ssl/star.example.com.crt SSLCertificateKeyFile /etc/ssl/star.example.com.key ServerName "two.example.com" DocumentRoot "/var/www/html/two" </virtualHost> <VirtualHost *:443> SSLEngine on >>other optional and required (SSL) directives<< SSLCertificateFile /etc/ssl/star.example.com.crt SSLCertificateKeyFile /etc/ssl/star.example.com.key ServerName "www.example.com" DocumentRoot "/var/www/html/www" </virtualHost> # Use the fact that the configuration file is parsed in order # and make this catch-all entry only catch what isn't defined above: <VirtualHost *:443> SSLEngine on >>other optional and required (SSL) directives<< SSLCertificateFile /etc/ssl/star.example.com.crt SSLCertificateKeyFile /etc/ssl/star.example.com.key ServerName "star.example.com" ServerAlias *.example.com Redirect / https://www.example.com </virtualHost> 

或者,如果你进入最低configuration: mod_vhost_alias可能是有趣的。