General information
An .htaccess (hypertext access) file, also known as a distributed configuration file, is a special file based on a regular expression parser that, using Apache’s mod_rewrite module, lets you define different configuration directives for each directory (and their subdirectories). In other words, it rewrites incoming URL requests to your server.
Note
When used correctly, mod_rewrite is very powerful. Beyond what we list below, there are many other applications for mod_rewrite. You can learn more about rewrite capabilities on apache.org.
.htaccess files are usually placed at the root of the directory where they should take effect. Once the file is uploaded to the desired directory, the rewrite rule should take effect immediately when accessing the site in a browser.
Warning
Some Content Management Systems (CMS), such as WordPress, overwrite .htaccess files with their own configuration. In that case, you may need to make these changes from the control panel of the CMS you are using.
Regular expressions
Rewrite rules often contain symbols that form a regular expression (regex). This is how the server knows exactly how you want the URL to change.
However, regular expressions can be hard to parse at first glance. Below are some common elements you will see in the rewrite rules provided in our articles related to .htaccess files.
- ^ – Indicates the start of the string to search.
- $ – Indicates the end of the string to search.
That is, ^directory1$ would match exactly directory1. - . – A dot matches any character except space (examples: a, B, 3).
-
* – An asterisk means the previous character can appear 0 or more times.
For example, ^uploads.*$ would match uploads2009, uploads2010, etc.Tip
If you don’t know what URL users will type, you can use ^.*$, which matches anything and nothing specific.
- () – Parentheses allow you to group and capture part of the first string for later use by referencing $1 in the second string. This is useful for handling requests to specific files that should be the same in both the old and new URL versions.
Additional information
Forgot your password?
You can find more information about regular expressions on perl.org.
General troubleshooting
404 Not Found
Examine the new URL in your browser carefully. Does it reference a file that exists on the server at the new location specified by the rewrite rule?
You may need to broaden the rewrite rule so it matches more results, or you may need to copy files from their original location to the new location so they can be served.
If the URL is simply incorrect, such as http://domain.com/directory1//file.html (in this case the error is the double “/” in the URL), you will need to review your .htaccess syntax again.
Warning
Clouding.io will not provide syntax support because the responsibility to review it lies with the customer.
Endless URL, timeout, redirect loop
If you notice an absurdly long URL, a page that does not load, or a browser error about redirection, you likely have redirect conflicts on the site.
You should review your entire .htaccess file, looking for rewrite rules that might clash with other rules. It may also be necessary to check .htaccess files in subdirectories. Note that FTP clients will not show .htaccess files unless you enable the option to view hidden files and folders.
Tip
Adding the [L] flag after a rewrite rule can help in some cases because it tells the server to stop attempting to rewrite a URL after that rule has been applied.
Practical examples of rewrite rules using .htaccess:
Note
The examples in the following links are provided as a courtesy. Clouding.io will not configure rewrite rules for individual customer websites.