The Experts

Making Sure htaccess and mod_rewrite files work

Most web hosting panels  makes extensive use of a common feature called 'url rewriting'. This basically means that you can request a pretty url like /page/about-this-website in your browser


Most web hosting panels  makes extensive use of a common feature called ‘url rewriting’. This basically means that you can request a pretty url like /page/about-this-website in your browser, and behind the scenes your web server will just ‘translate’ this to run the index.php with the correct parameters, so that your webhost can produce the correct page for you.

This doesn’t work out-of-the-box on all web servers, but it is a hard requirement for some. If you’re reading this page, you are likely in the process of setting up and after requesting the first page.

 

.htaccess or Hypertext Access File is a configuration file for Apache web servers that can be used to define very specific configuration options. Configurations can become quite granular with the use of regex however, most users typically stick to using popular .htaccess examples such as redirecting web pages, setting custom headers, etc.

Although .htaccess can be quite useful, it can also be somewhat of a challenge to figure out where the issue lies given you are faced with an .htaccess not working. This post provides a few tips for helping to resolve this issue by identifying a few common .htaccess problems that you can check in your own .htaccess file, as well as a few troubleshooting techniques.

 

Common .htaccess Problems

The following includes a few common .htaccess problems that are easy to fix and worth trying if you are experiencing issues with your .htaccess file not working.

 

.htaccess needs to be enabled with AllowOverride – This is the first thing that should be verified. If the AllowOverride directive is set to None then this will disable all .htaccess files. In order to verify this, you must open the Apache configuration file (typically either called httpd.conf or apache.conf) and check that the AllowOverride directive is set to AllowOverride All. If you needed to make changes to your Apache config, remember to save the file and restart Apache

 

“sudo service apache2 restart”

 

The filename is misspelled or does not begin with a “.” – If you are creating an .htaccess file from scratch (i.e. you are not using a CMS which comes with an .htaccess file included) then you must ensure that the filename is correct and that it begins with a period “.”. Without the period at the beginning, Apache will ignore the file – same goes for if the file is misspelled. Additionally, double check that the filename is all lowercase. Your .htaccess file should be named exactly like: .htaccess.

 

The location of your rules needs to be above or below others – Certain .htaccess rules may be sensitive to where they are located within the .htaccess file and therefore cause an .htaccess not working issue. If upon adding an .htaccess rule you notice that it is not taking effect, try moving it above the previous rule or to the very beginning of your file.

 

Conflicting .htaccess files – Although most users simply use one .htaccess file, you have the ability to use multiple. Since .htaccess file rules apply to the directory that they live in, as well as all other sub-directories, it can happen that 2 or more .htaccess files are conflicting with one another. To verify this, try disabling each additional .htaccess file you have one-by-one in order to see where the issue is.

 

Improper syntax being used – It is quite common for a syntax error to be the reason for an .htaccess file not working. If you are familiar with how to read and configure .htaccess rules, double check your configuration. Otherwise, you can use the troubleshooting tips mentioned in the next section to help determine why you are experiencing an issue.

 

How to Troubleshoot .htaccess Not Working

 

If you’re having issues with the actual syntax of your .htaccess file, you can run its contents through an .htaccess validator. The following tools will check your syntax and report back any errors that they find.

 

.htaccess Check – This first tool gives you 2 options for validating your .htaccess file. You can either copy / paste the contents of your file directly into the tool or upload your actual .htaccess file. The tool will then check your syntax and highlight any lines that it finds errors on.

 

How to check whether mod_rewrite is enable on server

  1. To check if mod_rewrite module is enabled, create a new php file in your root folder of your WAMP server. Enter the following : phpinfo();

2. Access your created file from your browser.

3. Ctrl+F to open a search. Search for ‘mod_rewrite’. If it is enabled you see it as ‘Loaded Modules’

4. If not, open httpd.conf (Apache Config file) and look for the following line.: #LoadModule rewrite_module modules/mod_rewrite.so

5. Remove the pound (‘#’) sign at the start and save the this file.

6. Restart your apache server.

7. Access the same php file in your browser.

8. Search for ‘mod_rewrite’ again. You should be able to find it now.


View Comments
There are currently no comments.