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 – Apache directive required by mod_rewrite.
- RewriteEngine On – Enables mod_rewrite.
-
RewriteRule – Defines a specific rule.
- The first pattern after
RewriteRulematches the original URL. - The second string defines the new URL.
- $1 – Special token that matches the parenthesized part of the first pattern and substitutes it in the target. It keeps subpages mapped to the same subpage instead of the directory root. Omit it to redirect to the directory root.
- [R=301,L] – Sends a 301 redirect and stops processing further rewrite rules. Recommended at the end of the last rule.
- The first pattern after
Additional information
Forgot your password?
You can find more details about special characters, known as regular expressions, in: Using rewrite rules in .htaccess