Ubuntu14.14开启ApacheRewrite功能的方法详解

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: |||超大  

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/*.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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航