mod_rewrite use in Apache 2.4
Solution 1
After enabled mod_rewrite by this command:
sudo a2enmod rewrite
Make file for your httpd configuration in /etc/apache2/sites-available/ so in my Ubuntu the file that I make is /etc/apache2/sites-available/code-machine.conf:
DocumentRoot /var/www
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Edit: And as reminded by Chris Gregory and firepol, you can register the new configuration file code-machine.conf:
sudo a2ensite code-machine.conf
Then restart your apache:
sudo service apache2 restart
Solution 2
even I was facing a similar issue with Apache 2.4 on 14.04
Please note, the configuration file is now located at /etc/apache2/apache2.conf
.
I tried this and it worked out for me.
sudo nano /etc/apache2/apache2.conf
Locate the directive Directory where your root directory is located and set the following:
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Solution 3
I had register the new conf file with Apache before it would work.
sudo a2ensite MyConfiguration.conf
Then I could run
sudo service apache2 restart
Related videos on Youtube
Tjita1
Updated on September 18, 2022Comments
-
Tjita1 over 1 year
Okay, so there are plenty of threads on how to use mod_rewrite, but I find it seems to work a bit differently in Apache 2.4. I tried this:
<Directory /var/www/vhosts/example.com> <IfModule mod_rewrite> RewriteEngine On RewriteBase /var/www/vhosts/example.com RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d </IfModule> </Directory>
This is situated in
/etc/apache2/conf-available/httpd.conf/
, and I've runtsudo a2enconf httpd.conf
which worked and enabled it, but it doesn't want to work anyways..I tried it without the
Directory
bit, I tried calling itmod_rewrite.c
which wordpress suggests, but neither of the above seems to work. I also tried it with theDirectory
tag but withoutIfModule
, which for obvious reasons ended up with Apache not starting.. And yes, I restart Apache between every attempt.Is there a new trick to this in Apache 2.4 that I don't know if? Any help appreciated.
-
OrangeTux about 10 yearsDo you have
mod_rewrite
installed?a2enmod rewrite
. -
Tjita1 about 10 yearsNope. Module mod_rewrite does not exist. How do I solve that then?
-
Tjita1 about 10 yearsYeah, I found that just now, thankyou. How should I write the httpd.conf to get it to work, then?
-
Tjita1 about 10 yearsOkay, so a friend of mine, pretty good at this, tells me it wont work if I put it in the httpd.conf, it has to be in a .htaccess to function. Issue is, I got the module running, have removed it from the httpd.conf and made a .htaccess, but it wont work anyways. What now?
-
Dan about 10 yearsWhat is the output of
ls /etc/apache2/sites-enabled
-
-
firepol over 7 yearsAs Chris Gregory reminded, register the new config file, in this case the code-machine.conf (I called it differently so adapt to your conf file): sudo a2ensite code-machine.conf Then: sudo service apache2 restart