Categories
Apache

The Six Most Common Htaccess Problems and How to Fix Them

Is your Apache htaccess not working? Here are the top reasons why an htaccess file may not work, and how get your htaccess working.

Htaccess Problem #1: No Dot Before the Filename

The .htaccess file’s filename must start with a dot, like so:

.htaccess

Files and directory names starting with a dot are treated as hidden files by Unix, Linux & Mac. The htaccess file is hidden so it doesn’t distract from normal web content like HTML files. See hidden files for more information.

Without the dot at the beginning, Apache will ignore the htaccess file.

Htaccess Problem #2: Filename Not All Lowercase

If an htaccess file’s name contains uppercase letters, it generally will not work on Linux or Unix. This is because filenames on Linux and Unix are usually case sensitive. If an htaccess file has any uppercase letters, e.g. .HTACCESS or .HTaccess, Apache won’t find the htaccess file. The htaccess is ignored.

An htaccess file containing uppercase letters generall will work on Windows and Mac. This is because filenames on these platforms are generally case insensitive. I’d recommend sticking with lowercase, so your websites are portable to Linux servers. The use of uppercase letters could also circumvent Apache directives designed to prevent the contents of .htaccess files read from the web.

Htaccess Problem #3: Filename Misspelt

Common misspellings of the htaccess file’s name are htacess and htacess. Check the filename has two c’s and two s’s.

Htaccess Problem #4: Htaccess Disabled by AllowOverride Setting

On some servers, Apache is configured to ignore some or all directives in .htaccess files. This is for security reasons. The AllowOverride directive controls which features will be allowed in .htaccess files. For example AllowOverride None can turn off htaccess files for a folder and its subfolders.

Check your Apache configuration file for which AllowOverride directive is applied to the directory containing your problem htaccess file.

If you’re not sure which configuration file to look in, start with the main Apache configuration file httpd.conf or apache2.conf. If your website is configured in a file included by httpd.conf (e.g. a virtual hosts configuration file), you will need to look in that file. See Location of httpd.conf on CentOS, Ubuntu, Mac and others to locate your httpd.conf.

To enable using a .htaccess file, change AllowOverride None to AllowOverride All.

For example, for a CentOS 5.3 server, I needed to change the AllowOverride setting in the file /etc/httpd/conf.d/virtualhosts.conf.

httpd.conf before:

Options FollowSymLinks
AllowOverride None

httpd.conf after:

Options FollowSymLinks
AllowOverride All

Be aware that enabling htaccess files has security implications, as htaccess files override your Apache configuration. For example, if your site provides uploads, a hacker could potentially upload a .htaccess file to your server and use it to gain access to your server. There are options to AllowOverride that restrict the directives that will be used from a .htaccess file. See the documentation for AllowOverride.

Htaccess Problem #5: Incorrect Syntax

If Apache can’t understand a line in your htaccess, it will usually cause an error from Apache. The error may show in the web browser when a webpage causes Apache to read the .htaccess file.

To demonstrate this, I added a bad line to the Smart Web Developer .htaccess file. See if you can spot the bad line. 🙂

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

utter rubbish config line here

This causes the following error in the browser:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, root@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

The reason for the error is logged to an Apache error log:

[Sat Feb 26 18:21:09 2011] [alert] [client 127.0.0.1] /Users/taz/Sites/smartwebdeveloper/.htaccess: Invalid command 'utter', perhaps misspelled or defined by a module not included in the server configuration, referer: http://smartwebdeveloper.com/

Another option to debug Apache syntax is to put the the .htaccess contents into the main Apache configuration file under a <Directory> directive. Apache has an option to parse and check its configuration files. To run an Apache syntax check, run: httpd -S.

Htaccess Problem #6: Htaccess Settings Overridden by Another Htaccess File

Multiple .htaccess files may be read and applied if a web request is made to a file in nested directories. Typically all .htaccess files in the path between the website’s root directory and the requested file’s directory will be read and applied in order. As a result, an htaccess file in a more deeply nested directory can override the settings made by an htaccess in a higher directory.

Is there another .htaccess file in another directory in the path to your webpage? That htaccess file the may be overriding the settings in the htaccess you’re looking at.

On Mac, Linux & Unix, you can find all .htaccess files on your website in the terminal:

find /path/to/website/root -iname .htaccess -print

Conclusion

Hope this article helped you! If it did, write me a comment and let me know.

Categories
Apache

How to Redirect a Domain Using an Apache .htaccess File

Why Redirect From Your www Domain to Your www-less Domain

For example, why would you redirect from www.smartwebdeveloper.com to smartwebdeveloper.com.

To search engines these are two different domains with identical content.

Your incoming links may get spread between the two domains – some links to pages in the www-prefixed domain and some links to the www-less domain.

Your SEO link juice gets spread between two sites. You could end up with two sites with two different page ranks, instead of a single site with a higher page rank than both.

Forward www to www-less Domain Using .htaccess

A good solution is to forward all pages at the www domain to the non-www domain. An HTTP response code of 301Moved Permanently – is the best response, because it says “this page isn’t here, this is it’s permanent URL”, because Google will still count any links to the www-prefixed domain towards your sites rankings.

To do this, in the root HTML directory for the www-prefixed domain, create a .htaccess file like the following:

#
# [taz] Redirect requests for www.smartwebdeveloper.com to
# smartwebdeveloper.com using Apache's rewrite engine.
#
RewriteEngine on

# [taz] Redirect requests for www.smartwebdeveloper.com,
# with an optional terminating '.' (DNS domains can be terminated with '.'),
# and optionally ending with :port (e.g. ':443' for SSL).
RewriteCond %{HTTP_HOST} ^www\.smartwebdeveloper\.com\.?(:[0-9]*)?$ [NC]

# [taz] Do a HTTP 301 redirect (i.e. page permanently moved)
# to the same URI at smartwebdeveloper.com.
RewriteRule ^(.*)$ http://smartwebdeveloper.com/$1 [R=301,L]

If you have troubles getting the .htaccess to work, try this .htaccess troubleshooting article.

Tested on CentOS 5.3.