Skip to content

mode_rewrite setup

August 25, 2007

mod_rewrite setup for rewrite engines

The mod_rewrite for rewrite engine can be enabled using a single command on Apache 2. Assuming you are running apache 2 it can be enabled by running the command
a2enmod rewrite
– Get the list of configured/enabled modules of the web server and check for “rewrite_module”.
Use the below command to get the list of enabled modules:
apache2ctl -M
If the module name is not found in the list, check the file “rewrite.load” in apache2 mods-available location.
The file should contain LoadModule rewrite_module modules/
If you find the file, then you have to enable/load the module by creating the soft link to the in “/etc/apache2/mods-enabled/”.
Also create a soft link of “/etc/apache2/mods-available/rewrite.conf” in “/etc/apache2/mods-enabled/”. Then restart the apache server and execute the command to list the enabled modules.

After enabling the module, there are two ways to enable the rewrite rules engines for a domain.

a. Add the following snippet in the httpd conf file
<Directory /var/www/>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
b. If you dont want to add the above snippet in the apache conf file, you may create a .htaccess file in the document root of your website. And place the above snippet. Also add the below lines in virtual host section of the httpd conf file.
AccessFileName .htaccess
<Directory /var/www/>
AllowOverride All
Then modify the .htaccess file based on your requirments.
If your site is running under virtual host like “” then
RewriteBase /
If your site located in a subdirectory at “”
RewriteBase /site1
Finally restart the apache web server, everything should work fine.
OR run ‘/etc/init.d/apache2 reload’ to activate new configuration!
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: