Anand Surampudi
Anand Surampudi

Reputation: 131

Whatever I do, .htaccess won't do the job

Working with .htacess file has always been a very frustrating experience for me. Someone please help.

This is what I want to achieve:

Here are my files:

Contents of My .htaccess file:

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

#301 Redirect Old File
Redirect 301 /index.html /maintenance.html

#Block users by IP
order allow,deny
deny from all 
allow from 123.456.789.0

Please help me understand:

Sorry for too many questions, but I really want know it all this time. Thanks in advance.

Upvotes: 0

Views: 49

Answers (2)

Anand Surampudi
Anand Surampudi

Reputation: 131

Thanks to @OlafDietsche and @ÁlvaroGonzález for this quick help. I am keeping their suggestions here so somebody like me will find it useful.

The problem is with my goals, not with the syntax. With their comments and answers, I came to know that my 2 goals were mutually contradicting ones.

I configured .htaccess to do both page-redirection and IP block. But if I am blocking (almost) everybody from accessing the site, page redirection makes no sense.

The required configuration in .htaccess is:

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

  #301 Redirect Old File
  Redirect 301 /index.html /maintenance.html

Upvotes: 1

Álvaro González
Álvaro González

Reputation: 146558

Is the location of each of the above files right? In what cases, the page ends up in 500 internal server error?

A "500 Internal Server Error" message means there's an error and you're expected to check the server logs for the exact details. Apache will not display the error message to be seen by everyone.

What changes should I make

It depends on what the problem is. If the problem is "500 Internal Server Error" that means that we still don't know what the problem is.

Is is necessary to run a2enmod rewrite?

That command enables mod_rewrite. You need to enable it if it isn't enabled. You don't need to enable it if it's already enabled.

It's worth noting that this command is not part of official Apache distribution. Some Linux distros (namely Debian and derivatives) change third-party packages to match their configuration preferences, as in this case.

Should I add <IfModule mod_rewrite.c> and </IfModule> as header and footer in any of the above config files?

As documentation explains, this block can be used to ignore directives when a given module is not installed. This can be useful for configuration templates to be distributed and optional features. In your case, it'll silently ignore your code if mod_rewrite is not available—you don't want that.


Last but not least:

order allow,deny
deny from all 
allow from 123.456.789.0

... belongs to the old (and really hard to understand) Apache/2.2 syntax. If you are using Apache/2.4* you may want to try Require.

(*) Some distros hate bundling recent software but 2.4 has been around for several years

Upvotes: 1

Related Questions