`
hooroger
  • 浏览: 13303 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Enable mod_rewrite in a Ubuntu server

    博客分类:
  • php
阅读更多

If you have done much work with Apache then most likely you have experienced a need for enabling the rewrite engine. If you are unfamiliar with the rewrite engine, you can think of it as a means for your web server to literally rewrite urls so they are easier for users to remember. So instead of having to enter:

 

http://192.168.1.100/rewrite.php?link=1

 

Your users would only have to type:

 

http://192.168.1.100/link2.html

 

Of course the rewrite engine serves as much more than just a means for your users to more easily remember addresses. Many server softwares (such as the social networking server Elgg) depending upon the rewrite engine in order to function. Because of this, the rewrite engine is almost a necessity to have working. By default Apache does not have the rewrite engine on, so you have to configure it to work. The goal of this tutorial is to show you how to enable the rewrite engine in a Ubuntu server installation. This article will assume you already have your Ubuntu server as well as Apache up and running.

 

 

What this involves

 

In order to get the rewrite engine working for your web site you will have to take care of the following steps:

 

Instruct Apache to load the mod_rewrite module.

Edit the document root entry so that the rewrite engine is allowed.

Create an .htaccess file to rewrite urls for you.

 

Creating a simple test

 

Before we get to the actual configuration, the first thing to do is to set up a little rewrite test. Open up a terminal window (or log into your GUI-less server) and change to the /var/www directory. In that directory create a file, named rewrite.php, with the following contents. NOTE: Code is located on pastebin.com for ease of use.

 

Copy and paste the contents of that file into your rewrite.php file. If you visit that page, in a browser, without the rewrite engine working, you will quickly see that it will not work. So, let's get rewrite working.

 

Now you will have to create an .htaccess file in the /var/www/ directory with the following contents:

 

RewriteEngine On

RewriteRule ^link([^/]*).html$ rewrite.php?link=$1 [L]

 

Enabling the module

 

In order to enable the mod_rewrite module in the Ubuntu server issue the following command:

 

sudo a2enmod rewrite

 

The above Apache2 Enable Module command will add the correct line in the /etc/apache2/apache2.conf file. That is the only change you need to make with the apache2.conf file. Now it's time to make a change to the document root.

 

In older versions of Apache all virtual host directory directives were managed in the /etc/apache2/apache2.conf file. This has changed. Now these alterations are handled within the /etc/apache2/sites-enabled/ directory. Within that directory you will find, by default, a single file called 000-default. If you open that file up for editing you will see, at the top, the two sections you need to edit in order to enable the rewrite engine for the document root of your Apache server.

 

First look in the <Directory /> section and change the line:

 

AllowOverride None

 

to

 

AllowOverride All

 

Do the same for the <Directory /var/www/> section.

 

Once you have the file edited, restart Apache with the command:

 

sudo /etc/init.d/apache2 restart

 

Now revisit the rewrite.php page in your browser and you should see that rewriting is now working. Congratulations, you have just taken on step forward in opening up an entire world for your Apache needs.

 

Final thoughts

 

The rewrite engine is used by so many web-based tools. Upon installation of Apache, this should be one of the first tasks you tackle.

 

Enjoyed the article?: Then sign-up for our free newsletter or RSS feed to kick off your day with the latest technology news and tips, or share the article with your friends and contacts on Facebook, Twitter or Google+ using the icons below.

分享到:
评论

相关推荐

    Apache服务器

    4. **设置开机启动**: 要使Apache在系统启动时自动运行,可以使用`sudo systemctl enable apache2`(Ubuntu/Debian)或`sudo systemctl enable httpd`(CentOS/RHEL)。 5. **测试运行**: 类似于Windows,访问`...

    linuxweb服务器配置

    此外,Apache有许多模块可以扩展其功能,如mod_rewrite用于URL重写,mod_proxy用于代理和负载均衡,mod_php或mod_python用于处理PHP或Python应用程序等。根据实际需求,选择合适的模块安装并配置。 总的来说,配置...

    apache服务器全能设置

    使用`systemctl enable apache2`(Ubuntu/Debian)或`systemctl enable httpd`(CentOS/RHEL)使Apache开机启动。安装`systemd`守护进程来监控Apache状态。 以上只是Apache服务器设置的基本知识点,实际操作中可能...

    建站资料——Apache建站配置指南

    sudo systemctl enable httpd 或 sudo systemctl enable apache2 ``` 二、配置Apache Apache的配置文件主要位于/etc/httpd/(CentOS/Fedora)或/etc/apache2/(Ubuntu/Debian)目录下。主配置文件为httpd.conf或...

    Apache2 for Linux

    - Ubuntu/Debian: `sudo systemctl enable apache2` - CentOS/RHEL: `sudo systemctl enable httpd` ### 二、配置Apache2 Apache的配置主要位于`/etc/apache2`(Ubuntu/Debian)或`/etc/httpd`(CentOS/RHEL)...

    Apache2.2+JBOSS 5 集群配置

    - 编译Apache,配置时指定模块如`–enable-module=so`、`–enable-module=setenvif`、`–enable-module=rewrite`和`–enable-proxy`等。 - 安装mod_jk模块,配置JK模块的worker.properties文件,定义JBOSS节点的...

    LINUX教程之22-apache服务器3

    例如,`mod_rewrite`模块用于URL重写,`mod_proxy`用于代理功能,`mod_php`或`mod_fcgid`用于处理PHP脚本。 最后,监控和日志管理是运维的重要部分。你可以通过`systemctl status apache2`检查Apache的状态,通过`...

    linux64位系统下安装apache2.2的一些心得收集

    Apache有许多可选模块,例如mod_rewrite用于URL重写,mod_php或mod_fcgid用于PHP支持,通过`a2enmod`命令可以启用它们。 12. **防火墙设置**: 记得在防火墙中打开Apache使用的端口(通常是80和443),使用`sudo ...

    在 Ubuntu 15.04/CentOS 7 中安装 Lighttpd Web 服务器的方法

    如果需要额外的功能支持,如 SSL、mod_rewrite 等,还需要额外配置相应的模块。 #### 三、设置 Lighttpd **1. 修改配置文件** - 默认情况下,Lighttpd 的配置文件位于 `/etc/lighttpd/lighttpd.conf`。可以通过...

    apache for linux

    Apache有许多可选模块,如mod_php用于处理PHP代码,mod_rewrite用于URL重写等。你可以通过`sudo yum install mod_name`命令安装所需模块。 **八、维护与升级** 定期检查Apache的更新,保持软件的安全性和稳定性。...

    wordpress Linux安装包

    &lt;IfModule mod_rewrite.c&gt; RewriteEngine On RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] ``` 六、启动和...

    ubuntu lighttpd+webpy (fastcgi)配置方法

    这种配置允许静态文件和动态内容在同一服务器上共存,无需额外配置`mod_rewrite`。需要注意的是,每次修改Python脚本后,都需要重启Lighttpd服务以应用更改。此外,可以在`10-fastcgi.conf`中配置多个FastCGI服务器...

    dns+apache+mysql+php+phpBB

    2. 配置Apache:修改配置文件`/etc/apache2/httpd.conf`(根据你的Linux发行版可能略有不同),开启必要的模块,例如`mod_rewrite`,以支持URL重写。 3. 启动Apache:运行`sudo systemctl start apache2`(或`sudo ...

    Bochs - The cross platform IA-32 (x86) emulator

    in order to enable X2APIC configure with --enable-x2apic - Implemented Intel VMXx2 extensions: - Enabled extended VMX capability MSRs - Implemented VMX controls for loading/storing of MSR_PAT and ...

    ubuntu下apache服务器操作方法小结

    Apache有许多可选模块,如mod_rewrite用于URL重写,mod_ssl用于HTTPS支持。可以通过`a2enmod`和`a2dismod`命令来启用和禁用模块。 11. **故障排除** 如果遇到问题,可以查看错误日志,使用`tail`命令跟踪实时日志...

    Apache 管理员手册.zip

    4. **模块管理**:Apache有许多可扩展模块,如mod_rewrite用于URL重写,mod_security用于安全防护。通过`a2enmod`和`a2dismod`命令来启用和禁用模块。 5. **日志管理**:Apache的日志文件默认位于`/var/log/apache2...

    Ubuntu 12.04 x64 编译安装lamp的步骤

    ./configure --prefix=/usr/local/apache2 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre --enable-so --enable-rewrite --enable-deflate --with-module=metadata...

    Apache安装httpd-2.4.16.tar.gz

    这里我们指定了安装路径,并启用了动态加载模块(`--enable-so`)和URL重写模块(`--enable-rewrite`)。 5. **编译与安装** 编译源码并安装: ``` make sudo make install ``` 6. **配置Apache** 复制示例...

    Linux下安装svn

    -prefix=/usr/local/apache --with-apr=/usr/local/apr/bin/apr-1-config --with-apr-util=/usr/local/apr/bin/apu-1-config --enable-modules=so --enable-dav --enable-maintainer-mode --enable-rewrite`进行配置...

    老司机传授Ubuntu下Apache+PHP+MySQL环境搭建攻略

    ./configure -prefix=/usr/local/apache -enable-module=so -enable-rewrite=shared -enable-authn-dbm make sudo make install ``` 4. 配置Apache的`httpd.conf`文件,打开文件后修改`ServerName`为`localhost...

Global site tag (gtag.js) - Google Analytics