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,其部分内容如下:
123456789101112131415# 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重定向:
123456Options +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才会生效。
1234567891011121314151617181920212223242526272829303132333435363738394041<VirtualHost12.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 from127.0.0.0/255.0.0.0::1/128</Directory></VirtualHost>配置完成后,重启 Apache,命令:/etc/init.d/apache2 restart
Ubuntu14.14开启Apache Rewrite功能的大屏查看
发布于:2015年12月21日
已被阅读次
- 最新发布
- linux下svn提交忽略某些文件... (215)
- 使用批处理来批量更新、提交SVN... (185)
- linux查看目录文件大小命令 (198)
- linux tar打包压缩排除某个... (173)
- Linux tar压缩和解压 (236)
- SVN子命令add用法浅析 (171)
- 热门博文
- 网友FBI探案:马蓉iPad惊人发现... (43392)
- 霍金携手俄罗斯富豪耗资1亿美元寻找外... (4795)
- 如何才能查看PHP内置函数源代码... (1250)
- 微信支付开发当前URL未注册的解决方... (623)
- 《谁为爱情买单》中的经典面试 ... (488)
- 让虚拟主机也用上SVN:适用于个人的... (433)
- 精华博文
- [推荐]Centos7 安装配置 SVN (202)
- easyswoole框架安装 (213)
- php开启pecl的支持(推荐) (204)
- 1-10个恋爱表现:男朋友爱你程度到... (206)
- 女生喜欢你的10个程度,到第六个就可... (180)
- Eclipse 没有Server选项... (257)
- 友情链接
- 我来忙 (110)