1

I have my website hosted on a CentOS server. When I add the .htaccess file it stops working showing "Internal Server Error".

On a side note: when I remove the .htaccess file the website works only with www.

I've checked and mod_rewrite is on and shared.

What can be wrong?

    //Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com[nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]

//Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>

//Prevent directory listings
Options All -Indexes
1
  • That means there is a problem with something in your .htaccess file. It isn't clear to me just by looking at it what it is. It could be a typo. It could be a module that isn't loaded by Apache. It could be that Apache is configured not to allow overrides. Do you have access to the error_log which will have more specific information? Have you tried putting just one rule into .htaccess at at time to see if any of them work on their own? May 5, 2019 at 9:41

1 Answer 1

3

I haven't checked the syntax on the rules, but my first reaction is that the comments look wrong. Try replacing the two forward slashes with a hash mark and make sure the # is the first character on the line. The rest looks fine, but I'd also remove the [nc] from the first RewriteCond line.

The code to prevent the viewing of .ht* files should also exist in the main apache config file too. It would be more secure to make it a server-wide setting and I would hope that it's the default for the package.

2
  • 1
    Good catch. .htaccess files do indeed use # comments, and not //. May 5, 2019 at 18:51
  • "remove the [nc] from the first RewriteCond line" - I wouldn't necessarily remove the [nc] flags argument altogether, however, a space delimiter is required before it (otherwise the rule simply won't do anything, since the condition will never match). However, the NC flag on the following RewriteRule is entirely superfluous, and there should probably be an L flag here in case any other directives are added in the future.
    – MrWhite
    May 5, 2019 at 23:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.