1.- What is a .htaccess?
The .htaccess (hyper-text access) is the default name of the Apache directory configuration file. It is used to customize the configuration of policies and parameters defined in the main configuration file of the hosting.
2.- How do we create and where do we place an .htaccess file?
To create an .htaccess file, open notepad and enter the necessary code. Save the file as a text file (.txt), for example "file_htaccess.txt", and upload it by FTP to the folder where it should be used. Once on the server, rename the file "file_htaccess.txt" to ".htaccess".
Warning: if you already have an .htaccess file inside your hosting, you can use it to make these settings.
The .htaccess must be placed inside the folder where you want it to take effect. For example, if you want to password protect a folder named "private", put the .htaccess inside the "private" folder.
The .htaccess file has a wide variety of uses and utilities that can be useful on the web. In the next tutorial we show some of the most used functions.
3.- Common .htaccess utilities
The .htaccess has a large number of utilities. In this tutorial we will show you some of the most common utilities and how you should configure them:
Redirect subdomain to subfolder
Redirect web using 301, 302 redirects
Redirect requests with www.example.org to example.org and vice versa
Redirect to secure HTTPS connection
Redirect users to use HTTPS for a particular folder
Improve character compatibility
Redirect subdomain to subfolder
In some situations you may need to create a redirect to a subdomain folder from .htaccess. If you want to indicate the subdomain "test.example.org", for example, to take you to the folder "example.org/test" you must indicate the following directives in the .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test.example.com$
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule (.*) /test/$1
The first line activates the redirection engine and it is only necessary to indicate it, if it has not been done before.
The third and fourth, perform the redirect.
And the second, it prevents the redirection from being performed again, if it is already in the "test" folder. This is necessary to avoid errors and loops as the .htaccess file is rerun when the request is redirected to the "test" folder.
Redirect web using 301, 302 redirects
If when accessing the web, you want to redirect the page to another:
RewriteEngine On
RewriteRule ^(.*)$ http:// www webaredirigir .com/$1 [R=301,L] (En "R=301", we can put the redirection method that we want '301, 302..')
If you have the web in a different subfolder than the "web" folder, you can redirect all queries to this subfolder:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.org\.org$
RewriteRule (.*) http:// www example .org/$1 [R=301,L]
RewriteRule ^$ folder [L]
Where:
example.org\.org would be, for example, cdmon.com\.com.
The folder would be, for example, "wordpress".
You can deny access to all users or allow access to our site to a certain group of IP addresses.
- To allow access to your site only to certain IPs:
ErrorDocument 403 http:// www example .org
Require all denied
Require ip 124.34.48.165
Require ip 102.54.68.123
The IPs 124.34.48.165 and 102.54.68.123 are the IPs that will have access to the web.
- Deny access to the site only to certain IPs:
Require all granted
Require not ip 145.186.14.122
Require not ip 124.15
The IP 145.186.14.122 and the 124.15.x.x network will not have access to the web.
Folder access control
A very common use of .htaccess is to prevent access to some folders. You may want to completely disable access to a folder, for example a folder with programming libraries that are included in the main files. In this case, only the main files will access them through the file system, but they will not be accessible via the web. You just create an .htaccess file in that folder that contains the following code if you want:
- Prohibit or completely disable access to a folder:
#deny all access
Require all denied
- Allow access from a specific IP:
#deny all access
Require all denied
Require ip 212.267.98.14
- Allow access to a specific range of IPs (forced through the netmask):
Require ip 192.168.0.0/24
- Block access to a specific file:
Require all denied
Folder listing
You can also use the .htaccess to display the contents of a folder in the directory structure.
Options +Indexes
- Folder listing, but you don't want the default icons to be displayed:
Options +Indexes
IndexOptions -FancyIndexing
- You might also want to avoid the folder listing:
IndexIgnore*
Redirect requests with www .example .org to example.org and vice versa
If you want your website to always be seen without www, we add the following code in the .htaccess:
RewriteEngine On
RewriteCond% {REQUEST_URI}!^/(Robots\.txt\favicon\.ico|sitemap\.xml)$
RewriteCond% {HTTP_HOST}!^Example \ .org $ [NC]
RewriteRule ^(.*)$ http ://example .org/$1 [R = 301,L]
On the contrary, that the web is always seen with www:
RewriteEngine On
RewriteCond% {REQUEST_URI}! ^ / (Robots \ .txt | favicon \ .ico | sitemap \ .xml) $
RewriteCond% {HTTP_HOST}!^www\.example \ .org$ [NC]
RewriteRule ^(.*)$ http:// www .example .org/$1 [R = 301, L]
Where:
example.org could be, for example, cdmon.com
Redirect SEO friendly website
If you have transferred domain names or want to redirect to a specific page without affecting the results of search engines like Google, you can use the following code:
Redirect 301 /d/filename.html http:// www example .org
Redirect web traffic
If what you want is that if someone tries to access a specific file to redirect them to another file, you can use the "redirect" directive:
Redirect /file.php http:// example .org/otros/nuevofichero.php
Redirect to secure HTTPS connection
If you want to redirect your entire site to a secure connection (HTTPS), this is very useful if, for example, you have an SSL certificate and you want to work with it.
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
Redirect users to use HTTPS for a particular folder
If you just want to redirect a particular folder to a secure connection (HTTPS) instead of the entire site, for example, you have a store in a folder other than your site, you have to use the following code:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} folder_name
RewriteRule ^(.*)$ https:// www .example .org/$1 [R,L]
Avoid hotlinking
Hotlinking consists of making a direct link to files, generally images and videos, that belong to another website. This practice consumes the transfer of the original image site. To prevent bandwidth "stealing" you can configure the .htaccess to prevent hotlinking.
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.org/.*$ [NC]
RewriteRule .*\.(jpe?g|gif|bmp|png)$ [L]
The last line is used to indicate what type of content you do not want (it is chosen according to the extensions of the files) that they take you:
RewriteRule .*\.(jpe?g|gif|bmp|png)$ [L]
Or, this other rule that also shows a file ("do_not_steal_bandwidth.gif" that you must have previously created) to warn that this image is being taken from your website.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/IMG/do_not_steal_bandwidth.gif$
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.org/.*$ [NC]
RewriteRule .*(gif|jpe?g|png|bmp)$ http ://example .org/IMG/no_robar_ancho_de_banda.gif [NC,R,L]
In the last line put the extensions of the files you want to prevent us from linking and indicate the warning image you want.
Change the default page
You can make the index page of your site to be displayed not the well-known "index". Here you set "about.html" as the home page:
#Serve Alternate Default Index Page
DirectoryIndex about.html
Forcing the search of your site
The following code will not directly increase the loading speed of the site in general, but it will load faster when the same user revisits it, by sending a 304 status when requesting items that have not been modified. You can change the caching frequency by changing the number of seconds (in this example it is once a day):
FileETag MTime Size
ExpiresActive on
ExpiresDefault "access plus 86400 seconds"
Improve character compatibility
This option is for those who do not use a standard character set recognized by the server, this measure avoids showing the famous error 500.
AddDefaultCharset utf-8
Create friendly URLs
A friendly URL consists of disguising a URL full of parameters to make it look more elegant and also to help the page rank better in search engines. Depending on how the web is programmed, the type of code you will have to put in will be one or the other.
Example 1:
For example, "folder/file.php?id=120&language=en" is an unfriendly URL, so we have to transform it into "folder/file/120/en" which is more elegant and helps the positioning in search engines. It is a friendly URL. With the .htaccess file you can make this "disguise" for the URL.
You have a folder on your website that has a file to which you pass parameters.
example.org/folder/file.php?id=25
Then, inside this folder you must create an .htaccess with the following code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule folder/(.*)/(.*).php$ /folder/file.php?id=$1
The last line is where you make the change.
(.*) will be the parameter you will pass to it, $1.
(.*).php$ will be the name of the file to "decorate" the URL.
Once created the .htaccess, in the link of your web you must put
Link to the file 25
Therefore, "folder/25/file-name.php" will be the same as "/folder/file.php?id=25"
Example 2:
If your website uses a URL like the following "http:// www .example .org/index.php?parametro=blog", you can convert it to "http:// www .example .org/blog".
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ /index.php?parameter=$1 [QSA,L]
Example 3:
Another example of conversion to friendly URLs.
Convert URL "http:// www .example .org/index.php?parametro=blog&paginador=12345" a "http:// www example .org/blog/pagina-12345".
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/page-([0-9]+)$ /index.php?parameter=$1&pager=$1 [QSA,L]
Example 4:
Code used by WordPress to make friendly URLs.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
For more information, you can contact us.