20

My htaccess file works on localhost but doesn't work when i deploy it to EC2 instance.

I'm using Macbook and in finder i cannot see the htaccess file, i thought that perhaps it didn't get copied to EC2 instance but i don't think this is the problem because when i copy the project i can see the htaccess file in my editor.

Is there something enabling mod rewrite in EC2 linux instance? If there is, i didn't do it or it enables mod rewrite as default?

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Yusuf Can Gürkan
  • 725
  • 2
  • 10
  • 20
  • 1
    FYI – you can show invisible files in OS X by running this Terminal command and restarting Finder. `defaults write com.apple.finder AppleShowAllFiles YES.` – zekel Jan 08 '16 at 19:11

7 Answers7

67

I pieced together some info from various posts so I thought I'd put up an answer.

  1. As Paul commented, If you're running Amazon EC2 Linux, you're probably running httpd instead of Apache. The file is therefore in /etc/httpd/conf/httpd.conf
  2. You have to change the file as root user. (from ssh access) Do this: sudo vim /etc/httpd/conf/httpd.conf (How to edit httpd.conf file in AMAZON EC2)
  3. DocumentRoot "/var/www/html" was listed in two places for me. I had to change the subsequent AllowOverride None to AllowOverride All in those two places.
  4. Restart apache. I restarted my whole ec2 instance (i have apache configured to start automatically) although just restarting apache should work. But I see the change is working.

I was trying to make the same changes in a .htaccess file (removing the index.php from urls in a code igniter application). Hope it helps!

Community
  • 1
  • 1
user1521567
  • 1,743
  • 3
  • 21
  • 31
  • Thank you so much for this. For others: **Restarting EC2 instance from AWS is a must**. – George Chalhoub Jun 27 '15 at 18:22
  • 4
    You don't need to restart the whole instance, just restart httpd. You can do so via the Command line with /sbin/service httpd restart – foamcow Feb 15 '16 at 18:19
  • Thanks a lot for this. – testuserx Jun 25 '16 at 09:00
  • 2
    Here we are 5 years after this answer was made. And people like me are still finding it as the solution. Makes you wonder why this information is so difficult to find. – durbnpoisn Jan 27 '19 at 16:16
  • 2
    Really helpful, But restarting httpd is must, to do so `sudo service httpd restart` – Hafiz Siddiq Oct 26 '19 at 21:53
  • Thanks. It worked. However, every time I deploy a new version, I have to do these steps because it overrides my changes. Does anyone know a way to persist them? And in my file, 'DocumentRoot` was not present. I had to add it myself. I am using AWS Beanstalk CLI to deploy new versions. – Wesley Gonçalves Oct 01 '20 at 18:46
  • Spot on solution. worked like a charm (01.2023). – Kosem Jan 31 '23 at 09:22
8

By default EC2 doesn't have .htaccess enabled, you must edit your httpd.config to allow for it.

In /etc/apache/sites-available/default change AllowOverRide = None to AllowOverRide = All.

  • Yes they are hidden but when i copy the folder hidden files are missed? So, i'm trying to understand that the problem is that i cannot copy my .htaccess file to server. – Yusuf Can Gürkan Oct 16 '13 at 16:11
  • Have you tried simply copying the contents of the .htaccess to .htaccess on the remote server? –  Oct 16 '13 at 16:12
  • I'm using Amazon EC2 Linux instance and i dont know how to do it. Is there a different .htaccess file in server besides i copied? – Yusuf Can Gürkan Oct 16 '13 at 16:16
  • 4
    If you're running Amazon EC2 Linux, you're probably running httpd instead of Apache. The file is therefore in `/etc/httpd/conf/httpd.conf` - be sure to amend both `` and `` as appropriate – Paul Gregory May 27 '14 at 12:50
2

Its a three step process

  1. Configure apache mod_rewrite,run in terminal. sudo a2enmod rewrite

  2. add the following code to /etc/apache2/sites-available/default

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>

3.Restart apache

/etc/init.d/apache2 restart
Abhishek Gupta
  • 4,066
  • 24
  • 27
0

There are three steps

  1. Configure apache mod_rewrite,run in terminal. sudo a2enmod rewrite

  2. Add this code in this file after closing VirtualHost tag /etc/apache2/sites-available/000-default.conf

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

  3. Restart apache server by command

    sudo service apache2 restart

0

When you using the apache2 server

Go to the following directory.

cd /etc/apache2/sites-available

If you use LS command here you will see the following file.

000-default.conf

That's the file with default apache configuration which are applied to your sites in /var/www/html folder.

Open this file for editing.

sudo nano 000-default.conf

Add following lines after DocumentRoot /var/www/html line.

Options FollowSymLinks AllowOverride All Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all

Save the file and restart the apache.

sudo service apache2 restart

that's it and now your .htaccess file will work

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Raghul SK
  • 1,256
  • 5
  • 22
  • 30
0

On Linux enviroment 1 - cd /etc/apache2/sites-available

2 - sudo nano 000-default.conf

and paste this code:

<Directory "/var/www/html">
    Order allow,deny
    Allow from all
    Options None
    AllowOverride All
</Directory>

This worked on AWS instance.

lucasvm1980
  • 660
  • 1
  • 9
  • 20
0

In my case, I am not running httpd, but only Apache.

I followed this website and it works.

Step 1: Enable mod_rewrite module in Apache

sudo a2enmod rewrite Then restart the Apache HTTP server with service command with sudo:

sudo service apache2 restart or using the systemctl command with sudo:

sudo systemctl restart apache2 Now that we have mod_rewrite enabled, we can proceed further to configure it.

Step 2: Enable the usage of the .htaccess file

sudo nano /etc/apache2/apache2.conf

Find the following section in /etc/apache2/apache2.conf. You should see the code like this:

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

and change, from AllowOverride None to AllowOverride All

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>
challet
  • 870
  • 9
  • 21
  • Hi your answer may address or not the question, but code formatting is a great way to make others quickly understand what you typed. Please format your code using the visual editor or markdown syntax. Docs [here](https://stackoverflow.com/help/formatting) – pierpy Jan 01 '23 at 07:27