Warning
The examples in the following links are provided as a courtesy – Clouding.io will not configure rewrite rules for its customers' individual websites.
General instructions
- Create a plain text file named .htaccess, or add the lines of code from the example that suits your needs at the top of your .htaccess file.
- Add the lines from the example appropriate for your needs to the .htaccess file. Keep in mind that you must replace the example text with your own information. That is, you should replace dominio.com with your domain name, directorio1 with your directory name, archivo.html with your file name, etc.
- Use your FTP client to upload the .htaccess file to the directory, or subdirectory, of the corresponding domain. If you want the file to load when accessing the domain, you should upload the .htaccess file to the root of your domain's hosting, which is usually:
- /var/www/vhosts/dominio.com/httpdocs – For a server with Plesk
- dominio.com/httpdocs – For shared hosting
Once the file is uploaded, the rewrite rule should work immediately.
Warning
Some Content Management Systems (CMS), such as WordPress, overwrite .htaccess files with their own configuration. In that case, you should look for a way to make these changes from the CMS you are using.
Example .htaccess file
Warning
dominio.com/httpdocs/directorio2/ must exist and have content in it for the following examples to work.
The following .htaccess file will redirect http://dominio.com/directorio1/ to http://directorio.com/.
Select this option if you want visitors to be redirected to the home page rather than to the individual pages in the old folder requested:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^directorio1.*$ http://dominio.com/ [R=301,L]
Test the examples
Create and upload the following “index.html” file to /directorio2 via FTP:
html
body
Mod_rewrite is working 🙂
body
html
Then, if you have followed the example correctly, when you visit http://dominio.com/directorio1/ in your browser, the redirect rule should redirect you to the URL http://dominio.com/, and you should be able to see the content of the test page.
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 replaces (or indicates) the part in parentheses specified in the first string. Basically, it ensures that subpages redirect to the same subpage rather than to the home page. You can omit it to redirect to the home page. (If you do not have the same content in the new directory as in the old directory, leave this regular expression.)
Additional information
You can find more details about special characters, known as regular expressions, in the article
-
[R=301,L] – This option performs a 301 redirect and also prevents rewrite rules written after it from affecting the URL (a good idea to add after the last rewrite rule).