【问题标题】:Move .htaccess content into vhost, for performance将 .htaccess 内容移动到虚拟主机中,以提高性能
【发布时间】:2011-08-30 10:10:40
【问题描述】:

我想知道如果我将 .htaccess 文件内容移动到 apache2 的 vhost 文件中是否可以提高性能?

这是我的 .htaccess 的内容

Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteCond %{SERVER_NAME} ^([^.]+\.[^.]+)$ [NC]
  RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

  # we check if the .html version is here (caching)
  RewriteRule ^$ index.html [QSA]
  RewriteRule ^([^.]+)$ $1.html [QSA]
  RewriteCond %{REQUEST_FILENAME} !-f

  # no, so we redirect to our front web controller
  RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

如果这样做是个好主意,我应该在 vhost 声明中的哪个位置放置内容上方?

谢谢!

【问题讨论】:

    标签: .htaccess apache2 vhosts


    【解决方案1】:

    如果您有可能编辑虚拟主机配置文件,您应该始终这样做。 .htaccess 会随着对您网站的每个请求进行解释,而另一方面 vhost.conf 仅在 httpd 重新启动/重新加载时进行解释。

    您可以在目录指令中设置Options - 例如:

    <Directory /usr/local/apache2/htdocs/somedir>
    Options +FollowSymLinks +ExecCGI
    </Directory>
    
    <VirtualHost [...]>
    [...]
    RewriteEngine On
    
    RewriteCond %{SERVER_NAME} ^([^.]+\.[^.]+)$ [NC]
    RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
    
    # we check if the .html version is here (caching)
    RewriteRule ^$ index.html [QSA]
    RewriteRule ^([^.]+)$ $1.html [QSA]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
    
    # no, so we redirect to our front web controller
    RewriteRule ^(.*)$ index.php [QSA,L]
    </VirtualHost>
    

    还可以看看 apache.org 上的这个 wikipost - 特别是 我应该什么时候使用,不应该使用 .htaccess 文件?

    【讨论】:

      猜你喜欢
      • 2021-12-06
      • 1970-01-01
      • 2011-07-28
      • 2014-04-22
      • 1970-01-01
      • 2014-08-06
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多