Amazon EC2 .htaccess and 500 Error

.htaccessamazon ec2apache-2.2httpd.confmod-rewrite

I have an Amazon EC2 (amzn-ami-pv-2013.09.0.x86_64-ebs (ami-149f7863)) on which I installed Apache, PHP and MySQL.

All works fine until I try to activate .htaccess.

I change AllOverride All in http.conf and restart the Apache service.

When I add the .htaccess file I receive a 500 Internal Server Error even if the htaccess is void.

mod_rewrite.so module is loaded

If I delete the htaccess all works fine.

What I'm doing wrong?

–EDITED–

This is the .htaccess content

RewriteEngine On
Options +FollowSymlinks
RewriteBase /

RewriteCond %{HTTP_HOST} ^mywebsite.com
RewriteRule (.*) http://www.mywebsite.com/$1 [R=301,L]

But even if I left the htaccess file whitout content it returns a 500 error.

The virtualhost definition is the following:

<VirtualHost *:80>
    ServerAdmin webmaster@mywebsite.com
    DocumentRoot /var/www/html/mywebsite.com
    ServerName mywebsite.com
</VirtualHost>

–EDITED-2-

I'm sorry but I sorted out this issue only creating the .htaccess file from command line.
Now it works. Thank you really for helping me.

–END-EDITING–

Thanks

Best Answer

What does your Apache error log say? Your site loads without the .htaccess in place, correct?

In my experience, when a 500 error is connected to .htaccess it could be something as simple as the .htaccess file itself getting munged on the file system. I would recommend deleting the actual file and creating a new .htaccess. If that still shows a 500 error, then go through the .htaccess line by line commenting out each line and reload the page. Chances are there is something odd in at least one line and this will show it.

EDIT: I Looked closer at your .htaccess content since you added it & think I spotted the issue. Change it to read like so:

Options +FollowSymlinks

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.mywebsite\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.mywebsite.com/$1 [L,R=301]

It seems like your formatting of the RewriteCond would choke. Also, I adjusted it to the way I would do it which reads in human form:

If this request is not to www.mywebsite.com then redirect it to www.mywebsite.com.

This way you are clearly grabbing anything and everything that is not www.mywebsite.com and sending them to the proper domain.

Also, in your Apache config I would recommend a ServerAlias to account for www.mywebsite.com. It seems like you are using NameVirtualHost so having the main server name be www.mywebsite.com with an alias being mywebsite.com makes the most sense.

<VirtualHost *:80>
    ServerAdmin webmaster@mywebsite.com
    ServerName www.mywebsite.com
    ServerAlias mywebsite.com
    DocumentRoot /var/www/html/mywebsite.com
</VirtualHost>
Related Topic