Ubuntu上開啓Apache Rewrite功能的方法

Ubuntu上開啓Apache Rewrite功能的方法html

 
本文介紹ubuntn 系統中開啓apache的urlrewrite功能的方法。
 
在Windows上開啓Apache的urlRewrite很是簡單,由於apache的用戶配置都是放在http.conf文件中,要開啓Rewrite功能,只須要把該文件中LoadModule rewrite_module modules/mod_rewrite.so前面的注視去掉,而後重啓APACHE便可。
 
但在Ubuntu上則有所不一樣,默認Apache包配置是按照目錄和文件存放的,/etc/apache2目錄包含conf.d、mods-available、mods-enabled、sites-available、sites-enabled文件夾,apache2.conf、envvars、httpd.conf(用戶配置文件)、magic、ports.conf(APACHE端口配置)配置文件。
 
1、Ubuntu默認未開啓Rewrite支持
 
其中,mods-available是指可用的模塊,mods-enabled是指當前已經默認加載的模塊。httpd.conf默認是個空文件,由於大部分加載工做都被分散到不一樣的配置文件裏,整體加載配置文件是apache2.conf,其部份內容以下:
# 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模塊加載,加載開啓過程以下。
 
2、手動開啓加載Rewrite
 
一、使用終端工具鏈接服務器,輸入管理員賬號和密碼
 
二、執行加載Rewrite模塊:
a2enmod rewrite
執行後,會提示OK和重啓Apache命令(/etc/init.d/apache2 restart)。
 
三、參照上文的目錄配置,作個啓動連接(下次啓動自動加載):
 
ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load
  
 
執行後會在mods-available目錄下建立一個快捷方式,鏈接到mods-enabled下rewrite模塊。
 
四、重啓apache:
 
/etc/init.d/apache2 restart
 
3、單一缺省網站配置及重定向參考
 
若是隻有一個網站,且默認使用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>
相關文章
相關標籤/搜索