How to enable mod_rewrite for Apache 2.2
Asked Answered
O

15

547

I've got fresh install of Apache 2.2 on my Vista machine, everything works fine, except mod rewrite.

I've uncommented

LoadModule rewrite_module modules/mod_rewrite.s

but none of my rewrite rules works, even simple ones like

RewriteRule not_found %{DOCUMENT_ROOT}/index.php?page=404

All the rules I'm using are working on my hosting, so they should be ok, so my question is, is there any hidden thing in apache configuration, that could block mod rewrite?

Omophagia answered 15/5, 2009 at 14:37 Comment(6)
actually the problem was that i had wrong path, because ${DOCUMENT_ROOT} pointed me to root directory which was ok on hosting, but wrong on local, so the problem wasnt just RewriteEngine On, which i already had ..Omophagia
Can you please change the accepted answer to this one? The author of current accepted answer has admitted it's not good enough.Stormie
How to enable mod_rewrite in Ubuntu 12.04 LTS (should work in Debian too). This shows installation AND activation, the tutorials below surprisingly don't do :(Galantine
I would strongly advise everybody that wants to enable mod_rewrite directives in .htacces files to use AllowOverride FileInfo instead of allowing everything, as ALL the answers on this page suggest. Sad to see all these answers take the as-long-as-it-works-it's-okay-approach instead of trying to understand the consequences of the proposed 'solution'. Try to understand what you are doing on your server and how to limit the privileges you just gave away with AllowOverride All. RTFM!, it is quit clear on the subject.Haun
Where did you uncommented it? Where is the file?Kind
@Kind It's in the Apache config file named httpd.conf. In my PC, it is in the directory C:\xampp\apache\conf, because I installed Apache with XAMPP.Politics
H
1137

In order to use mod_rewrite you can type the following command in the terminal:

sudo a2enmod rewrite

Restart apache2 after

sudo /etc/init.d/apache2 restart

or

sudo service apache2 restart

or as per new unified System Control Way

sudo systemctl restart apache2

Then, if you'd like, you can use the following .htaccess file.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

The above .htaccess file (if placed in your DocumentRoot) will redirect all traffic to an index.php file in the DocumentRoot unless the file exists.

So, let's say you have the following directory structure and httpdocs is the DocumentRoot

httpdocs/
    .htaccess
    index.php
    images/
        hello.png
    js/
        jquery.js
    css/
        style.css
includes/
    app/
        app.php

Any file that exists in httpdocs will be served to the requester using the .htaccess shown above, however, everything else will be redirected to httpdocs/index.php. Your application files in includes/app will not be accessible.

Hbomb answered 22/4, 2011 at 18:14 Comment(13)
The ideas here are correct for a UNIX CLI but the commands only work for a Debian based Linux distributions and not a Windows environment of the OP.Whyte
a2enmod rewrite did the jobSpoliate
Further note: you also need to make sure that your site's configuration has AllowOverride All in order for the .htaccess file to be read at all.Halfdan
How do you turn mod_rewrite off, in other words, reverse the action by a2enmod rewrite?Rasputin
@Rasputin you can use a2dismod rewriteHbomb
Use AllowOverride FileInfo rather than All in your site-configuration if you don't need to allow any other overrides.Obolus
@Lamy /etc/init.d/httpd restart works with Redhat based systems and old apache versionsNissie
how to do it in online project?Frazee
how can I use a2enmod rewrite in windows?Indiscernible
This is the best answer to a totally different question, found when I googled "debian install mod rewrite"Ul
What change needs to be made to make includes/app as accessible?Prom
I also referred to this askubuntu.com/questions/833040/…Prager
is the command essential or can it be done purely from a conf file?Soares
D
247

For my situation, I had

RewriteEngine On

in my .htaccess, along with the module being loaded, and it was not working.

The solution to my problem was to edit my vhost entry to inlcude

AllowOverride all

in the <Directory> section for the site in question.

Diaster answered 24/2, 2011 at 19:25 Comment(7)
Indeed, even with a2enmod rewrite & RewriteEngine On in the .htaccess - the existence of AllowOverride all in the VirtualHost file will prevent it from working - this is what caught me out, changing it to AllowOverride all worked like a charm :)Lansquenet
beginner here.. where's the VirtualHost file ?Weaken
THIS is the way to go... I had everything else set except for this.Jansson
The AllowOverride directive determines the "Types of directives that are allowed in .htaccess files". Furthermore, "When this directive is set to None, then .htaccess files are completely ignored. In this case, the server will not even attempt to read .htaccess files in the filesystem." Official docs: httpd.apache.org/docs/2.2/mod/core.html#allowoverridePodium
This fixed it for me -- I had an issue with WordPress pretty permalinks not working properly. If you do a2enmod rewriteand then in the VirtualHosts file change 'AllowOverride None' to 'AllowOverride All' it should work. The VirtualHosts file applies if you are running multiple domain names to the same server (and IP address). You would have set it up to set up the domain.Arnold
in what file should I include "AllowOverride all" I'm testing this in my personal computer running ubuntu and apache, but eventually I want to upload to share hosting thanks?Associate
In Ubuntu, the path is /etc/apache2/apache2.confJobye
L
97

Try setting: AllowOverride All.


Second most common issue is not having mod rewrite enabled: a2enmod rewrite and then restart apache.

Liripipe answered 4/11, 2011 at 3:30 Comment(3)
This should be the #1 answer as it solves a problem that is happening in a default ubuntu installation!Galantine
This will allow .htaccess to override any other apache configuration. This is not recommended.Helpful
Yes :) this was my problem too, but here is the place that you are explaining for rookies like me that this "AllowOverride All" is in /etc/apache2/sites-available/[sitename].conf and [sitename-ssl].conf. I had to do a second research because of you :D, thanks by the way!Wangle
M
92

If non of the above works try editing /etc/apache2/sites-enabled/000-default

almost at the top you will find

<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

Change the AllowOverride None to AllowOverride All

this worked for me

Meetly answered 7/8, 2012 at 12:54 Comment(4)
Changed error from /laravel/public/authors was not found on this server to /index.php was not found on this serverTorpid
cant find < Directory >, only <VirtualHost *:80>Mcdaniels
@NicolasS.Xu place this in the <VirtualHost *:80> tagDenote
I've done the same but added a new section <Directory /var/www/html> to apply the rule from this folder and up only.Steapsin
D
59

New apache version has change in some way. If your apache version is 2.4 then you have to go to /etc/apache2/. There will be a file named apache2.conf. You have to edit that one(you should have root permission). Change directory text like this

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

Now restart apache.

service apache2 reload

Hope it works.

Demiurge answered 18/6, 2015 at 6:9 Comment(3)
This is the up to date answer.Pandorapandour
If you are in 2019 and beyond, please upvote this answer :)Tomchay
Yes sir, this helped me out of a jam! Simplest of all answers.Elidiaelie
V
49

In Ubuntu:

Run:

a2enmod rewrite

and then:

service apache2 restart

mod_rewrite will now be enabled!

Vibrissa answered 11/7, 2012 at 15:25 Comment(0)
N
35

There are many ways how you can fix this issue, if you know the root of the issue.

Problem 1

Firstly, it may be a problem with your apache not having the mod_rewrite.c module installed or enabled.

For this reason, you would have to enable it as follows

  1. Open up your console and type into it, this:

    sudo a2enmod rewrite

  2. Restart your apache server.

    service apache2 restart

Problem 2

  1. You may also, in addition to the above, if it does not work, have to change the override rule from the apache conf file (either apache2.conf, http.conf , or 000-default file).

  2. Locate "Directory /var/www/"

  3. Change the "Override None" to "Override All"

Problem 3

If you get an error stating rewrite module is not found, then probably your userdir module is not enabled. For this reason you need to enable it.

  1. Type this into the console:

    sudo a2enmod userdir

  2. Then try enabling the rewrite module if still not enabled (as mentioned above).

To read further on this, you can visit this site: http://seventhsoulmountain.blogspot.com/2014/02/wordpress-permalink-ubuntu-problem-solutions.html

Nil answered 9/2, 2014 at 12:11 Comment(2)
Great breakdown of the issue.Gall
Don't use AllowOverride All as it will enable too many dangerours features (like using perl scripts). Instead use AllowOveride OptionsDependable
C
33

Open terminal and typin a2enmod rewrite, It will enable your mod_rewrite module for Apache.

Then go to /etc/apache2/sites-available and edit default file. (For this you must have writable permissions to this file and sites-available folder.)

Replace below with existing lines 4 to 14

DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>

Now restart your apache by /etc/init.d/apache2 restart or service apache2 restart

Take clean URL test again and this time it will be passed.

Colloquialism answered 4/7, 2014 at 10:6 Comment(0)
P
15

<edit>

Just noticed you said mod_rewrite.s instead of mod_rewrite.so - hope that's a typo in your question and not in the httpd.conf file! :)

</edit>

I'm more used to using Apache on Linux, but I had to do this the other day.

First off, take a look in your Apache install directory. (I'll be assuming you installed it to "C:\Program Files" here)

Take a look in the folder: "C:\Program Files\Apache Software Foundation\Apache2.2\modules" and make sure that there's a file called mod_rewrite.so in there. (It should be, it's provided as part of the default install.

Next, open up "C:\Program Files\Apache Software Foundation\Apache2.2\conf" and open httpd.conf. Make sure the line:

#LoadModule rewrite_module modules/mod_rewrite.so

is uncommented:

LoadModule rewrite_module modules/mod_rewrite.so

Also, if you want to enable the RewriteEngine by default, you might want to add something like

<IfModule mod_rewrite>
    RewriteEngine On
</IfModule>

to the end of your httpd.conf file.

If not, make sure you specify

RewriteEngine On

somewhere in your .htaccess file.

Parmentier answered 16/5, 2009 at 22:17 Comment(1)
@gabehabe your avatar just just looked like it was holding an icon my doc , lolImplement
S
10

Use below command

sudo a2enmod rewrite

And the restart apache through below command

sudo service apache2 restart
Stipendiary answered 12/9, 2018 at 13:37 Comment(0)
S
8

I just did this

sudo a2enmod rewrite

then you have to restart the apache service by following command

sudo service apache2 restart
Scotch answered 12/10, 2014 at 11:0 Comment(0)
O
7

Old thread, just want to put that don't set AllowOverride to all instead use specific mod you want to use,

AllowOverride mod_rewrite mod_mime

And this line should be un-commented

LoadModule rewrite_module modules/mod_rewrite.so

Refrences

Orinasal answered 27/3, 2017 at 9:3 Comment(0)
A
4

The first time I struggled with mod_rewrite rules ignoring my traffic, I learned (frustratingly) that I had placed them in the wrong <VirtualHost>, which meant that my traffic would ignore all of them no matter how well-written they were. Make sure this isn't happening to you:

# Change the log location to suit your system. RewriteLog /var/log/apache-rw.log RewriteLogLevel 2

These parameters will activate if you perform a graceful restart of Apache, so you can recycle them in and closely monitor the mod_rewrite behavior. Once your problem is fixed, turn the RewriteLogLevel back down and celebrate.

In 100% of my experience, I've found that the RewriteLog has helped me discover the problem with my rewrite rules. I can't recommend this enough. Good luck in your troubleshooting!

Also, this bookmark is your best friend: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritelog

Ailina answered 29/4, 2015 at 9:29 Comment(1)
This was removed and is no longer available in 2.4 (httpd.apache.org/docs/2.4/mod/mod_rewrite.html)Neal
T
3

There's obviously more than one way to do it, but I would suggest using the more standard:

ErrorDocument 404 /index.php?page=404
Twelvemo answered 15/5, 2009 at 15:52 Comment(0)
C
3

What worked for me (in ubuntu):

sudo su
cd /etc/apache2/mods-enabled
ln ../mods-available/rewrite.load rewrite.load

Also, as already mentioned, make sure AllowOverride all is set in the relevant section of /etc/apache2/sites-available/default

Catlin answered 7/2, 2014 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.