将.htaccess内容移动到vhost中以获得性能

时间:2011-05-22 00:10:10

标签: .htaccess apache2 vhosts

我想知道如果将.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声明的哪个位置置于内容之上?

谢谢!

1 个答案:

答案 0 :(得分:12)

如果您可以编辑vhost配置文件,则应始终这样做。 .htaccess将被解释为对您的站点发出的每个请求,而另一方面,vhost.conf仅在httpd restart / reload上解释。

您可以在Directory-directive中设置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文件吗?

相关问题