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 best suits your needs at the top of your .htaccess.
- 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.
- Upload the .htaccess file via FTP to the directory or subdirectory of the corresponding domain. If you want the file to load when accessing the domain, for example, dominio.com. You must upload the file to its root directory, which is usually:
/var/www/vhosts/dominio.com/httpdocs – For a server with Plesk
dominio.com/httpdocs – For shared hosting
Once the file has been 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 may need to find a way to make these changes from your CMS control panel.
Example .htaccess file
The following example .htaccess file will redirect http://dominio.com/ to http://www.dominio.com/. It will also work if an individual file is requested, such as http://dominio.com/archivo.html:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^dominio.com [NC]
RewriteRule ^(.*)$ http://www.dominio.com/$1 [R=301,L]
Test the examples
Simply access http://tudominio.com from any browser, and you should see the same page displayed, but the address has changed to http://www.dominio.com.
Also, any URL such as http://dominio.com/archivo.html will be converted to http://www.dominio.com/archivo.html.
Code explanation
- Options +FollowSymLinks – is an Apache directive, a prerequisite for mod_rewrite.
- RewriteEngine On – enables mod_rewrite.
-
RewriteCond %{HTTP_HOST} – specifies which URLs you want to pass, or not pass, through the specified rule. In this case, you would be specifying that the rule should work with dominio.com.
- [NC] – makes the rule work with both uppercase and lowercase versions of the URL.
-
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 and not 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 afterward from affecting the URL (a good idea to add after the last rewrite rule).
-