Enable mod_rewrite in a Ubuntu server

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值