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

# 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>

Ubuntu上开启Apache Rewrite功能的方法的更多相关文章

  1. 如何开启Apache Rewrite功能

    一.Ubuntu默认未开启Rewrite支持 apche模块加载工作已分散到不同的配置文件,这样看起来似乎更为合理,管理起来也非常方便.下面看一下如何开启Rewrite模块,当用户需使用301重定向. ...

  2. 在Ubuntu中开启Soft AP功能

    在Ubuntu中开启Soft AP功能 1.查看采用的无线网卡是否支持Soft AP: 注意,可以看到有AP字样,表明支持.楼主比较背,在易迅上挑了个销量最高的netcore nw360,结果无法搭建 ...

  3. APACHE 服务器开启URL REWRITE模块的方法

    最近做wordpress,发现固定链接总是设置不了.后来发现是由于apache服务器的URL REWIRITE模块没有开启导致. 查询了资料之后终于设置成功,记录下步骤: 1:开启apache的url ...

  4. Ubuntu中开启MySQL远程访问功能,并将另一个数据库服务器中的数据迁移到新的服务器中

    一.开启MyS远程访问功能 1.进入服务器输入netstat -an | grep 3306确认3306是否对外开放,MySQL默认状态下是不对外开放访问功能的.输入以上命令之后如果端口没有被开放就会 ...

  5. Windows7上开启ftp服务器功能

    开启ftp服务功能   1 进入“控制面板”->“程序”->"打开或关闭Windows功能",找到“Internet信息服务”选项 2 将“Internet信息服务”选 ...

  6. activate mod_rewrite How To Set Up mod_rewrite for Apache on Ubuntu 14.04 Apache Rewrite url重定向功能的简单配置

    https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite-for-apache-on-ubuntu-14-0 ...

  7. 手把手教你在Ubuntu上安装Apache、MySql和PHP

    1:首先安装apache:打开终端(ctrl+Alt+t), 输入命令:sudo apt-get install apache2即可安装, 安装完后,打开浏览器,在地址栏输入:localhost或者h ...

  8. 手把手教你在ubuntu上安装apache和mysql和php

    1:首先安装apache:打开终端(ctrl+Alt+t), 输入命令:sudo apt-get install apache2即可安装, 安装完后,打开浏览器,在地址栏输入:localhost或者h ...

  9. centos 开启apache rewrite模式

    mod_rewrite能使网页伪静态,对于搜索引擎友好,下面就是开启这个功能的说明!启用mod_rewrite模块在conf目录的httpd.conf文件中找到 LoadModule rewrite_ ...

随机推荐

  1. solr集群安装部署

    一.安装部署zookeeper集群 zookeeper集群 二.solr集群部署 集群配置 IP | 节点名称 | 环境 --- | --- | --- 192.168.137.128 | 192.1 ...

  2. SQL Server 代理(已禁用代理 XP)

    sp_configure 'show advanced options', 1; GO RECONFIGURE WITH OVERRIDE; GO sp_configure 'Agent XPs', ...

  3. 【Android】完善Android学习(五:API 3.2)

    备注:之前Android入门学习的书籍使用的是杨丰盛的<Android应用开发揭秘>,这本书是基于Android 2.2API的,目前Android已经到4.4了,更新了很多的API,也增 ...

  4. C11内存管理之道:智能指针

    1.shared_ptr共享智能指针 std::shared_ptr使用引用计数,每个shared_ptr的拷贝都指向相同的内存,在最后一个shared_ptr析构的时候,内存才会释放. 1.1 基本 ...

  5. redis cluster以及master-slave在windows下环境搭建

    一.redis cluster环境搭建: 1.了解Redis Cluster原理: 详细了解可参考:http://doc.redisfans.com/topic/cluster-tutorial.ht ...

  6. Ubuntu12.04 安装LAMP及phpmyadmin

    1.安装 Apache apt-get install apache2 2.安装 PHP5 apt-get install php5 libapache2-mod-php5 3.安装 MySQL ap ...

  7. shell 25个常用命令

    1.列出所有目录使用量,并按大小排序.   ls|xargs du -h|sort -rn #不递归下级目录使用du -sh 2.查看文件排除以#开关和空白行,适合查看配置文件.   egrep -v ...

  8. IIS 搭建

    1. 在打开程序功能里面,点击IIS安装.注意要选择适当的各种有用的服务.例如默认文档就需要安装非IIS下面的选项. 2. IIS部署网站可以参考网上的步骤.会遇到500处理程序“Extensionl ...

  9. Tomcat 7下如何利用 catalina.properties 部署公用类

    Tomcat 有很多配置文件,其中一个是  catalina.properties ,本文介绍catalina.properties 中的设置项. 一.组成   catalina.properties ...

  10. ZOJ3874 Permutation Graph

    Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward has a permutation {a1, a2, … an}. He finds ...