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
The directory /directorio1 must be unique in the URL. That is, for example, the redirect would not work if the file “directorio1.html” existed, as in the following URL: http://dominio.com/directorio1/directorio1.html.
Also, for the rule to work, the directory directorio1 must exist and have content in it.
The following example .htaccess file will redirect http://dominio.com/archivo.html to http://dominio.com/directorio1/archivo.html:
File
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} dominio.com$ [NC]
RewriteCond %{HTTP_HOST} !directorio1
RewriteRule ^(.*)$ http://dominio.com/directorio1/$1 [R=301,L]
Testing the examples
Create and upload the following “index.html” file to /directorio1 via FTP:
File
html
body
Mod_rewrite is working 🙂
body
html
Then, if you have followed the example correctly, when you visit http://dominio.com/ in your browser, you should see the address change to http://dominio.com/directorio1/, along with the contents of the test page.
Code explanation
- Options +FollowSymLinks – is an Apache directive, a prerequisite for mod_rewrite.
- RewriteEngine On – enables mod_rewrite.
-
RewriteCond %{HTTP_HOST} – is used to indicate which URLs we want, and do not want, to go through the specified rule
- In the first case, we want the rule to work with dominio.com
- [NC] – makes the rule work with both uppercase and lowercase versions of the URL.
- ! – means “not”, and is used so that the rule does not work, in this case, with directorio1. We specify this because we do not want to rewrite a URL that already includes directorio1, because it would keep trying to add directorio1 to the URL, and it would become an infinite URL.
- In the first case, we want the rule to work with dominio.com
-
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 main page. You can omit it to redirect to the main page. (If the same content that was in the old directory does not exist in the new directory, leave this regular expression.)
Additional information
Forgot your password?
You can find more details about special characters, known as regular expressions, in the article Using rewrite rules
-
- [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).