Enable mod_rewrite on Apache EC2 Linux Server

linux

Today I was working on a client’s Amazon Linux EC2 web server and had to enable mod_rewrite. This tutorial should help anyone else trying to do the same thing.

linux

I read online that mod_rewrite was probably the culprit. Before assuming this, you should make sure your .htaccess file is working properly with correct permissions. You can simply make sure the file exists and set permissions to 777, test, then set your permissions back as they were to make your file secure once again. If that doesn’t do it, then check your mod_rewrite using the following procedure:

  1. make a info.php file with only this for content: “<?php phpinfo(); ?>” and save it as info.php. Upload to your root directory and go to your domain name/info.php and search for the text “mod_rewrite” if you find it in the php info file, then your mod_rewrite is enabled. If not, then enable it.
  2. To enable mod_rewrite, try the command line command: sudo a2enmod rewrite

If mod_rewite was enabled then something else is wrong. Check your etc/httpd/conf/httpd.conf file. It should have the following in it:

AllowOverride All

Basically, you’ll be adding All insted of None to AllowOverride, you do not want to edit the main directory configuration, you want to edit the one that looks like this:

<Directory “/var/www/html”>

Not:

<Directory />

Then restart Apache with:

sudo service httpd restart

from the command line.

Now, if you are using WordPress and trying to enable Permalinks as I was, next, go to your wp-admin page and go to settings/permalinks and change from default to post-name and click the save button to save the changes and try again. If you did this before making mod_rewrite fixes, you may need to change back to default, save, and then change to post-name again and save one last time for the settings to work.

Good Luck!

(Source: http://jafty.com/blog/enable-mod_rewrite-on-apache-ec2-linux-server/)