Notice
The examples in the following links are provided as a courtesy – Clouding.io will not configure rewrite rules for individual customer websites.
General instructions
- Create a plain text file named .htaccess, or add the example lines that fit your needs at the top of your existing .htaccess file.
- Add the lines from the appropriate example to the .htaccess file. Replace the placeholder text with your own data. For example, replace domain.com with your domain name, directory1 with your directory name, file.html with your file name, etc.
- Use your FTP client to upload the .htaccess file to the directory, or subdirectory, of the relevant domain. If you want the file to apply site-wide, upload it to the document root of your hosting, typically:
/var/www/vhosts/domain.com/httpdocs – For a server with Plesk
domain.com/httpdocs – For shared hosting
After uploading the file, the rewrite rule should take effect immediately.
Notice
Some Content Management Systems (CMS), such as WordPress, overwrite .htaccess with their own configuration. In that case, make the change from within your CMS.
.htaccess file example
Notice
domain.com/httpdocs/directory2/ must exist and contain content for the following examples to work.
-
Redirect http://domain.com/directory1/ to http://domain.com/directory2/. Choose this if the two directories do not share the same file structure:
Options +FollowSymLinks RewriteEngine On RewriteRule ^directory1.*$ http://domain.com/directory2/ [R=301,L]
-
Redirect http://domain.com/directory1/file.html to http://domain.com/directory2/file.html. Choose this if your content is duplicated in both directories:
Options +FollowSymLinks RewriteEngine On RewriteRule ^directory1/(.*)$ http://domain.com/directory2/$1 [R=301,L]
-
Test the examples
Create and upload the following “index.html” to /directory2 via FTP:
html
body
Mod_rewrite is working 🙂
body
html
If you followed the first example, visit http://domain.com/directory1/ in your browser. If the redirect rule works, you should see the URL change to http://domain.com/directory2/ and the test page content.
If you followed the second example, visit http://domain.com/directory1/index.html. If the redirect rule works, you should be redirected to http://domain.com/directory2/index.html and see the test page content.
Code explanation
- Options +FollowSymLinks – is an Apache directive, a prerequisite for mod_rewrite.
- RewriteEngine On – enables mod_rewrite.
-
RewriteRule – defines a particular rule.
- Within the rewrite rule, the first character string after RewriteRule defines what the original URL looks like.
- The second string after RewriteRule defines the new URL.
-
$1 – This special character matches (and
replaces) the part in parentheses specified in the
first string. Basically, what it does is make sure that
sub-pages redirect to the same sub-page and not
to the main page. You can omit it to redirect to the
main page. (If you don't have the same content in the
new directory as there was in the old directory, leave
this regular expression.)
Additional Information
Have you forgotten your password?
You can find more details about special characters, known as regular expressions, in the article: Using rewrite rules in .htaccess
- [R=301,L] – This option performs a 301 redirect, and also prevents rewrite rules written afterwards from affecting the URL (a good idea to add after the last rewrite rule).