Ubuntu14.14开启Apache Rewrite功能的

发布于:2015年12月21日 已被阅读

  • Ubuntu上开启Apache Rewrite功能的方法

     

    本文介绍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,其部分内容如下:

     

    ?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    # 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重定向:

     

    ?

    1
    2
    3
    4
    5
    6
    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才会生效。

    ?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    <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