Make sure .htaccess is working with Allowoverride All
June 30, 2022
Test if .htaccess
is working
The simplest way to test if apache uses your .htaccess
file, or if it otherwise ignores it, is to intentionally break it.
Edit the .htaccess
file, so the first line reads ‘Test.’:
Test.
# Set the default handler
DirectoryIndex index.php index.html index.htm
...
Now, if you refresh the page in your browser, you should see an error page like this:
If you see this error, that’s actually good! This means that Apache is parsing the .htaccess
file, and it encounters the error we’ve put in there! So far, so good!
If you do not see an ‘Internal Server Error’, your Apache setup ignores the .htaccess
file, and you need to fix that. Generally, Apache ignores the .htaccess
file because of the following Apache configuration AllowOverride none
. Check your virtual host configuration and add/amend to AllowOverride All
.
Example:
<Directory /var/www/site/example.com/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Test if mod_rewrite
is working
To test if mod_rewrite
is working correctly, do the following:
- Edit
.htaccess
file with the contents as below.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^.*$ /redirected
</IfModule>
- In your browser, add the URL like
http://yourdomain/test
. - If the URL is redirected to
http://yourdomain/redirected
it means that themod_rewrite
is working.
My htaccess is broken? What do?
There are many reasons why it might not work on your system, and these reasons vary so wildly, that we can’t give an exhaustive solution for that. That said, here are a few pointers that might help you fix it:
Enable .htaccess
in your httpd.conf
or apache.conf
It’s unusual, but possible that .htaccess
is not enabled on your site. If you are hosting it yourself, it’s easy enough to fix. Open your httpd.conf
or apache.conf
in a text editor, and locate the <Directory>
section:
<Directory "/var/www/htdocs">
AllowOverride None
Change the AllowOverride
line to:
AllowOverride All
Be sure to restart Apache after making any modifications to this file. Now, your .htaccess
should work. You can also make this change inside a virtual host, which would normally be preferable, but that depends on the way Apache is set up.
If your site is hosted elsewhere, check your control panel (Plesk, DirectAdmin, CPanel, whatever) to see if you can enable .htaccess
there. If not, contact your hosting provider to do it for you.