Block access to pages, redirect and enable cache
-
This thread is part of the guide to set up a server on Debian.
Remember that the content of Hardlimit is under Creative Commons license.
Once we have the.htaccess file operational, we can start using it for some practical issues that are explained below.
Block access to a page with.htaccess
One of the possibilities that the.htaccess file allows us is to prevent access to a subfolder or file on our web server.
If we do not yet have a.htaccess file, we create it in the subfolder where we want it to take effect.
touch /var/www/html/subcarpeta/.htaccess nano /var/www/html/subcarpeta/.htaccessTo block access to a file, we paste the following text at the end of the file:
<Files archivo.html> deny from all </Files>To prevent access to a folder, we paste this:
<Directory /var/www/html/subcarpeta/bloqueada> deny from all </Directory>Move page address
On occasion, we may want to move our website from one address to another, either because we want to change its name, because we want to move from a subdomain to a domain, or for any other reason.
So that search engine robots and users can continue to access the content without needing to know the new address, we can redirect all content to the new domain. This way, web browsers and robots will receive a status code 301 moved permanently, which indicates that the site has been moved.
The only thing we have to do is create a.htaccess file in the subfolder where the website is located:
touch /var/www/html/subcarpeta/.htaccess nano /var/www/html/subcarpeta/.htaccessFinally, we paste this text:
Options +FollowSymLinks RewriteEngine on RewriteRule (.*) https://nuevo_dominio.com/$1 [R=301,L]The only thing we must modify is the domain of our new website. Now, every time someone accesses the old address, they will be immediately redirected to the new one, and search engine robots will know that the site has been moved.
Enable browser caching
If we enable browser caching with Apache, we will save a large amount of bandwidth when we have to serve certain elements, and on the other hand, the client will load the page much faster. However, we must bear in mind that if we allow certain files to be cached, the client could be viewing content without contacting the server, so these would be uncounted visits. In addition, if there are certain elements that change frequently, in the case that this resource has been accessed previously, the changes will not be visualized until the cache expires.
First, we activate the expires module and restart Apache as root:
a2enmod expires /etc/init.d/apache2 restartIf we do not have a.htaccess file, we create it:
touch /var/www/html/subcarpeta/.htaccess nano /var/www/html/subcarpeta/.htaccessAnd we add the following text:
<IfModule mod_expires.c> <filesmatch "\\.(jpg|JPG|jpeg|JPEG|gif|GIF|png|PNG|css|ico)$"> ExpiresActive on ExpiresDefault "access plus 30 day" </filesmatch> </IfModule>The parameters that interest us are these:
· filesmatch: this indicates the extensions of the files that will be stored in the browser cache. Do not add the html extension since these are files that consume little bandwidth and any modification will not take effect in the visualization until the cache expires.
· ExpiresDefault "access plus xx day": this indicates the number of days the cache will be stored. The more time, the less often certain files will be sent to regular visitors, but the longer it will take for them to be able to visualize the changes made to those files. -
C cobito referenced this topic on