Ubuntu14.14開啟Apache Rewrite功能的方法詳解,ubuntu14.14rewrite

來源:互聯網
上載者:User

Ubuntu14.14開啟Apache Rewrite功能的方法詳解,ubuntu14.14rewrite

Ubuntu上開啟Apache Rewrite功能的方法
發布時間:2012-09-11編輯:指令碼學堂
本文介紹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連接埠配置)設定檔。

一、Ubuntu預設未開啟Rewrite支援

其中,mods-available是指可用的模組,mods-enabled是指當前已經預設載入的模組。httpd.conf預設是個空檔案,因為大部分載入工作都被分散到不同的設定檔裡,總體載入設定檔是apache2.conf,其部分內容如下:

 

# Include module configuration:Include /etc/apache2/mods-enabled/*.loadInclude /etc/apache2/mods-enabled/*.conf# Include all the user configurations:Include /etc/apache2/httpd.conf# Include ports listingInclude /etc/apache2/ports.conf......# Include generic snippets of statementsInclude /etc/apache2/conf.d/# Include the virtual host configurations:Include /etc/apache2/sites-enabled/

  


從這些語句可以看出,載入工作已經分散到不同的設定檔,這樣看起來似乎更為合理,管理起來也非常方便。下面看一下如何開啟Rewrite模組,當使用者需使用301重新導向、偽靜態等Rewrite功能時,一般都習慣於使用.htaccess檔案配置,比如下面的301重新導向:

 

Options +FollowSymLinksRewriteEngine onRewriteCond %{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@localhostDocumentRoot /var/www<Directory />Options FollowSymLinksAllowOverride None</Directory><Directory /var/www/>Options Indexes FollowSymLinks MultiViewsAllowOverride NoneOrder allow,denyallow from all</Directory>ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/<Directory "/usr/lib/cgi-bin">AllowOverride NoneOptions +ExecCGI -MultiViews +SymLinksIfOwnerMatchOrder allow,denyAllow from all</Directory>ErrorLog /var/log/apache2/error.log# Possible values include: debug, info, notice, warn, error, crit,# alert, emerg.LogLevel warnCustomLog /var/log/apache2/access.log combinedAlias /doc/ "/usr/share/doc/"<Directory "/usr/share/doc/">Options Indexes MultiViews FollowSymLinksAllowOverride NoneOrder deny,allowDeny from allAllow from 127.0.0.0/255.0.0.0 ::1/128</Directory></VirtualHost>

  

配置完成後,重啟 Apache,命令:/etc/init.d/apache2 restart

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.