# Include module configuration: Include /etc/apache2/mods-enabled/*.load Include /etc/apache2/mods-enabled/*.conf # Include all the user configurations: Include /etc/apache2/httpd.conf # Include ports listing Include /etc/apache2/ports.conf ...... # Include generic snippets of statements Include /etc/apache2/conf.d/ # Include the virtual host configurations: Include /etc/apache2/sites-enabled/
從這些語句可以看出,加載工作已經分散到不同的配置文件,這樣看起來似乎更為合理,管理起來也非常方便。下面看一下如何開啟Rewrite模塊,當用戶需使用301重定向、偽靜態等Rewrite功能時,一般都習慣於使用.htaccess文件配置,比如下面的301重定向:
Options +FollowSymLinks RewriteEngine on RewriteCond %{HTTP_HOST} ^abc.com [NC] RewriteRule ^(.*)$ http://www.shouce.ren/$1 [L,R=301] RewriteCond %{HTTP_HOST} ^www.abc.com[NC] RewriteRule ^(.*)$ http://www.shouce.ren/$1 [L,R=301]
配置完成後,使用/etc/init.d/apache2 reload命令加載生效,這時,如果未開啟Rewrite功能,則會出現500錯誤(浏覽器顯示),查看LOG錯誤如下: [Sun Jan 30 02:41:29 2011] [alert] [client 12.34.56.78] /srv/www/shouce.ren/public_html/.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration 說明需要手動開啟Rewrite模塊加載,加載開啟過程如下。 二、手動開啟加載Rewrite 1、使用終端工具連接服務器,輸入管理員帳號和密碼 2、執行加載Rewrite模塊: a2enmod rewrite 執行後,會提示OK和重啟Apache命令(/etc/init.d/apache2 restart)。 3、參照上文的目錄配置,做個啟動鏈接(下次啟動自動加載): ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load 執行後會在mods-available目錄下創建一個快捷方式,連接到mods-enabled下rewrite模塊。 4、重啟apache: /etc/init.d/apache2 restart 三、單一缺省網站配置及重定向參考 如果只有一個網站,且默認使用apache分配的默認www文件夾(沒有創建單獨的配置文件,比如/sites-availbe/shouce.ren),可能還需要修改/etc/apache2/sites-available/default這個文件,把其中的AllowOverride None修改為AllowOverride All,因為default配置裡還默認關閉.htaccess重載,打開後.htaccess才會生效。
<VirtualHost 12.34.56.78:80> ServerAdmin webmaster@localhost DocumentRoot /var/www <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all </Directory> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all </Directory> ErrorLog /var/log/apache2/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog /var/log/apache2/access.log combined Alias /doc/ "/usr/share/doc/" <Directory "/usr/share/doc/"> Options Indexes MultiViews FollowSymLinks AllowOverride None Order deny,allow Deny from all Allow from 127.0.0.0/255.0.0.0 ::1/128 </Directory> </VirtualHost>
配置完成後,重啟 Apache,命令:/etc/init.d/apache2 restart