Catalogue
What to Do When .htaccess Doesn't Work or Has No Effect

What to Do When .htaccess Doesn't Work or Has No Effect

🌐 日本語で読む

Overview

This is about what to do when things don’t behave as configured in your .htaccess, or when it looks like the file isn’t being read at all.

For cases like “mod_rewrite redirects work over http but not over https,”
most common misconfiguration patterns can be resolved by checking the following basics.

Test Environment

  • CentOS 6.6 (Final)
  • Apache/2.2.15(Unix)

What You Need in Order to Use .htaccess at All

You need the following directive in a configuration file such as /etc/httpd/conf/httpd.conf.

1
AllowOverride All

What You Need in Order to Use mod_rewrite

To use mod_rewrite, which is used very frequently, you need the following.

1. Install mod_rewrite.so

2. Load mod_rewrite.so from the Apache configuration file

Let’s start by checking the above.

1. Verify mod_rewrite.so Is Installed

With Apache, it’s usually stored under the modules directory.

1
2
$ ls -al /etc/httpd/modules/mod_rewrite.so
-rwxr-xr-x 1 root root 60464 10月 16 23:49 2014 /etc/httpd/modules/mod_rewrite.so

2. Verify mod_rewrite.so Is Loaded from the Apache Configuration File

In an Apache configuration file such as
/etc/httpd/conf/httpd.conf or /etc/httpd/conf.d/*.conf, the following is configured.

  • Note: In some environments the configuration file is not placed in /etc/httpd/conf/httpd.conf,
     so treat this as a general example only.
1
LoadModule rewrite_module modules/mod_rewrite.so

Additional Notes

The module-loading configuration usually has
AllowOverride All set within a <Directory> directive, like the following.

In the example below, under the “/var/www/html” directory, the .htaccess you place there takes priority for configuration.

1
2
3
4
5
6
<Directory "/var/www/html">
AllowOverride All
Options -Indexes FollowSymLinks Includes ExecCGI
Order allow,deny
Allow from all
</Directory>

In the unlikely event that the mod_rewrite.so module does not exist, you will need to recompile Apache.

Installing mod_rewrite into Apache and Recompiling

  • Recompile and restart Apache.
1
2
3
4
5
$ sudo su -
# cd <path to httpd source>
# ./configure –enable-ssl=shared –enable-rewrite –enable-deflate –enable-headers –enable-so
# make & make install
# service httpd restart

That’s all.

kenzo0107

kenzo0107