Enable Apache mod_rewrite on Ubuntu Linux system

Apache Logo

Any LAMP (Linux Apache MySQL PHP) web development effort requires fully functional Linux web server. You can also develop using LAMP installation on other operating systems but that just isn't the real thing. The way I develop is by using Ubuntu LAMP local server I can play with without fear of cutting off hundreds of people from any public site just because I've typed colon instead semicolon somewhere in some php file. If you want to know how to setup Ubuntu Linux for your development work here's one of my earlier articles on that topic:

Ubuntu Netbeans and LAMP server with Xdebug as non-root user

In this article I will show you how to enable Apache mod_rewrite engine on your Ubuntu LAMP installation. This module is used by web administrators to "mask" their site URLs into the form they want users to see. This is done because it is not necessary or secure to expose your site innerworkings like GET parametars or scripts paths on your server.

mod_rewrite is also used by WordPress for the permalinks feature. So if you are testing WordPress locally and you want to enable permalinks feature you must enable mod_rewrite on your local server. 404 page displayed when you try to access any WordPress page or post after enabling permalinks is there to remind you to enable mod_rewrite.

In this article I will mostly write like you've followed my LAMP setup article where I've shown you how to setup Ubuntu LAMP server with document root in your own home directory. First we will enable mod_rewrite using following command:

sudo a2enmod rewrite

mod_rewrite rules are usually specified using hidden .htaccess file used to override global server settings for particular web site. In the default Ubuntu LAMP web site configuration file this override isn't allowed . Because of that we must open our site config file and change AllowOverride directive for our site document root from None to All.

If you've followed my LAMP setup article you will use this command to open your site config file:

gksudo gedit /etc/apache2/sites-available/public_html

If you've been using default Ubuntu LAMP server config file with document root in /var/www you will use this command:

gksudo gedit /etc/apache2/sites-available/default

If you've been using Apache 2 userdir module (you can read my article on how to enable userdir Apache module) you should edit this file:

gksudo gedit /etc/apache2/mods-enabled/userdir.conf

If you've copied the default site configuration into your own configuration file you must open that configuration file. In this case you should explore /etc/apache2/sites-available/ directory to see all available web site configurations so you could open the right one.

Now let's change AllowOverride directive for your site document root from None to All. If you've followed my LAMP setup article you will find this directive on the line 11 from the public_html file we've opened in the last step. public_html file is displayed inside the following code block:

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 *:80>
	ServerAdmin webmaster@localhost
 
	DocumentRoot /home/marko/public_html
	<Directory />
		Options FollowSymLinks
		AllowOverride None
	</Directory>
	<Directory /home/marko/public_html/>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride All
		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 /home/marko/public_html/error.log
 
	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn
 
	CustomLog /home/marko/public_html/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>

Now we need to restart Apache server to apply our new settings:

sudo service apache2 restart

That's it. Now feel free to have fun developing using your Ubuntu Linux system!

DevGenii

A quality focused Magento specialized web development agency. Get in touch!

8 thoughts on “Enable Apache mod_rewrite on Ubuntu Linux system

  1. dane

    Found this via a google search of ‘ubuntu mod_rewrite enable’ (you were the 2nd hit I believe… nice SEO). Thanks for laying it out so clearly.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *