How to redirect content with the RedirectMatch directive
As the name suggests RedirectMatch is a directive that's used for redirecting content. It's part of mod_alias which itself is an Apache module. All HostKnox servers have mod_alias installed.
You can use RedirectMatch to redirect content both to an external URL address or to another folder on the same hosting account. With the RedirectMatch directive you can set up more complicated redirecting rules than with the Redirect directive. For more basic information on redirecting and on the Redirect directive check out the article on how to redirect content with the Redirect directive.
The RedirectMatch directive is followed by the location of the content that you want to redirect and then the destination URL (or folder, if its on the same account). For example, if you put the following rule in the .htaccess file that's in the public_html directory on your hosting account:#Redirect all HTML files from yourdomain.com to yourdomain2.com RedirectMatch (.*)\.html$ http://yourdomain2.com$1.html
all HTML files will be redirected to their exact match on yourdomain2.com. So, if a client requests yourdomain.com/index.html, they will be redirected to yourdomain2.com/index.html; if they request yourdomain.com/files/example.html, they will be redirected to yourdomain2.com/files/example.html.
When specifying the source and the destination path, Perl Compatible Regular Expressions (PCRE) are used. They are often referred to as just regular expressions. Complex and flexible redirect rules can be created with the help of these expressions. In the above example, for instance, the string (.*) stands for any number of characters, so that it includes any filename within public_html and all its subfolders (no matter how many levels down). The backslash before .html is there to indicate that the dot should be interpreted as a dot because it's a special character (meta-character) that's otherwise used to indicate any character. The $ character at the end is used as an anchor to indicate the end of the source path. So the whole source path (.*)\.html$ actually means any HTML file within the public_html folder and all its subfolders. For some more information on the most commonly used PCRE characters and what they mean check out the article on the most common regular expression characters.
Regular expressions are used only in the path that specifies the content to be redirected. The destination path is just a plain URL or a local path on the same hosting account. What's interesting in the destination URL is the part $1 that's right after the URL address and before the extension for the file type (e.g. .html). What this means is that whatever is matched by the string (.*) in the source path will be put in the place of $1 in the destination URL. So using the above example, if (.*) matches /files/example, then the destination URL would become http://yourdomain2.com/files/example.html.
Of course, you can also redirect only files that are located in a specific subfolder within public_html on your hosting account. For example, if you want to redirect only the HTML files that are located in public_html/files to a folder called pages on yourdomain2.com, the directive would look like this:#Redirect the HTML files from yourdomain.com/files to yourdomain2.com/pages RedirectMatch ^/files/(.*)\.html$ http://yourdomain2.com/pages/$1.html
Notice that in this second example where the path on your hosting account is a subfolder, the path begins with the character ^. This is also an anchoring character that marks the beginning of the path. In the case with the destination URL this anchoring character is not necessary.
You can also use the RedirectMatch directive to redirect content to another folder within the same hosting account. For example, if you want to redirect all HTML files from public_html/files to public_html/files2/new, the directive would look like this:#Redirect HTML files from public_html/files to public_html/files2/new within the account RedirectMatch ^/files/(.*)\.html$ /files2/new/$1.html
Note that in this case you don't have to put the anchoring character ^ before the path to which the content is redirected.
Keep also in mind that the directives are processed in the order in which they appear in the .htaccess file. Those closer to the top of the file are processed first. This means that a preceding redirect rule can make another one that appears after it useless (or lead to errors) depending on the particular rules.
By default, if you don't specify the HTTP response status, a temporary redirect status is used (HTTP response status code 302). If you want to, you can, for example, change the redirect status to permanent (code 301) or to see other (303). To learn how to do it check out the article on changing the HTTP redirect status codes.
For even more complex redirect rules you can use mod_rewrite. This is an Apache module for redirecting and rewriting. For more information you can visit the mod_rewrite section of our knowledgebase, as well as the mod_rewrite tutorial.
Was this answer helpful?
- How to block access to folders on your account
- What are .htaccess files and how to create and edit them
- How to change the error message for the different error codes and redirect errors
- How to prevent people from using images from your site as inline images on theirs
- How to display directory indexes with the Options directive