Posted on 08 Oct, 2007 by Paul
I recently had some problems getting mod_rewrite to work but in the end I got there. Although the module was loading, the rewrite rules were not taking affect and I was just getting 404s.
For others having the same problem, firstly check that the module is set to be loaded by apache in the configuration file. On openSUSE 10.2 this is in the file /etc/apache2/sysconfig.d/loadmodule.conf and specifically the line:
LoadModule rewrite_module /usr/lib/apache2-prefork/mod_rewrite.soAdditionally, the file /etc/sysconfig/apache2 should have rewrite in the line beginning:
APACHE_MODULES=If you’re not using openSUSE 10.2 then check the documentation coming with your distribution for where module loads need to be located. Finally, restart apache with one of the following commands (depending upon your Linux distribution) as root in a console/xterm:
~$ rcapache2 restart ~$ /etc/init.d/apache2 restartThe module should now be loaded however you need to check if it was successful as it appears to be (assuming there were no failure messages when restarting apache). This can be done with a simple php file. Create a new text file containing the following:
<?php phpinfo(); ?>Save this as phpinfo.php to your public_html (or whatever your document root is) and open it in a webbrowser. This will display a lot of set-up information about php and apache2. Use the browser’s find command to locate rewrite. Assuming you’re successful in finding it then the module is loaded. The next step is to test out a rule. Save the following to you public_html/document root as .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (.*)phpinfo.html$ phpinfo.php [QSA,NC,L]
</IfModule>
and enter http://localhost/~user/phpinfo.html (correct the url as required) into a browser and assuming the page loads then the rewrite worked and your system is set up correctly.
However, if you get a 403, 500 or any other message check that your .htaccess file is correct, unless it is a 404 and you believe everything to be correct. If the error is 404 then you need to dive in to your apache configuration again and ensure that AllowOverride All is set either globally or for the relevant directory entry. Often AllowOverride is disabled by default, but ensure it is changed for a directory declaration that will impact where you are trying to set up mod_rewrite to work and not for the apache icons directory or something else equally as daft.
Finally, restart apache again and all should be working fine. If it’s not, check the apache documentation and look carefully through the configuration files as AllowOverride may have been enabled for the incorrect directory. If you have any questions, leave a comment and I’ll try my best to assist.
Category: Linux | Comments: none









Write a comment: