Magento htaccess file: Definition and how to use it to redirect 301 in Magento 2

Magento htaccess file

Magento htaccess file or a Hypertext Access file is a configuration text file that controls directories and subdirectories and allows altering them. A permanent 301 redirection in your htaccess tells the search engines that an old link is replaced by the new one. It’s the recommended way to direct traffic from an existing page to the new one.  In this post today, we will introduce to you every thing about Magento htaccess file  and how to use it to redirect 301 in Magento 2. Let’s check them out!

What is Magento htaccess file?

If you don’t know, the file is especially important for the Search Engine Friendly URLs as it’s implemented for keeping mod-rewrite rules. Also, it contain a standard web server and php directives, which can be modified for enhancement of a website performance. You will have to the backup of the htaccess file Magento 2 htaccess file sample (.htaccess.sample) when it is required to extract suitable code lines or to substitute it instead of the current when it won’t work after an update. More than that, you can look through some other main .htaccess usage scenarios.

Protect the website.

  • To restrict the directory access via the following command:
deny from all
  • Remember that this one restricts access to anyone including you. To enable access for some particular IP apply this command:
order deny,allow 
deny from all
allow from xxx.xxx.xxx.xxx
  • To blacklist some IPs, you can use the below command:
order deny,allow 
allow from all
deny from xxx.xxx.xxx.xxx
  • To restrict access by password, you add these directives to the htaccess:
require valid-user
Authname "Password Required"
Authtype Basic
AuthUserFile "/www/pwd/.htpasswd"
  • The htpasswd file should be generated as well. This text file is the password list with the below structure:
user1:password
user2:password

Here the password is encoded. One can find plenty of online passwords creator like this. The line

test:$apr1$3gKh3mag$KrgTcxAqx4EeMVP//3wc80

Forbid Hotlinking.

Hotlinking means the directs links from site A to non-html objects including  images, movie files, etc. on site B. This can considerably impact bandwidth usage and CPU load. You can stop these requests on your server by adding these directives to your htaccess:

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]

Note: yourdomain.com need to be changed to the corresponding name of your domain

Optimize the website

  • Apache server can use many external modules that extend the default function. One of them is mod_gzip. You can compress your HTML, JS and CSS files. For this add these directives to htaccess
<ifModule mod_gzip.c>
	mod_gzip_on Yes
	mod_gzip_dechunk Yes
	mod_gzip_item_include  \.(html?|txt|css|js|php|pl)$
	mod_gzip_item_include handler ^cgi-script$
	mod_gzip_item_include mime ^text/.*
	mod_gzip_item_include mime ^application/x-javascript.*
	mod_gzip_item_exclude mime ^image/.*
	mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
  • Usage of the caches can be specified by means of the mod_expires extension. Apply this sample code:
<ifModule mod_expires.c>
        ExpiresActive On
        ExpiresDefault "access plus 1 seconds"
        ExpiresByType text/html "access plus 1 seconds"
        ExpiresByType image/gif "access plus 2592000 seconds"
        ExpiresByType image/jpeg "access plus 2592000 seconds"
        ExpiresByType image/png "access plus 2592000 seconds"
        ExpiresByType text/css "access plus 604800 seconds"
        ExpiresByType text/javascript "access plus 216000 seconds"
        ExpiresByType application/x-javascript "access plus 216000 seconds"
</ifModule>

How to enable Magento developer mode htaccess?

At first, you should  check everything is prepared well

  • Disable Cache System > Cache Management > Select All [Check-Boxes] > Actions = Disable > Submit
  • Re-Index All System > Index Management > Select All [Check-Boxes] > Actions = Reindex Data > Submit
  • Disable Complication System > Tools > Compilation > Disable
  • Turn On Logging System > Configuration > Advanced > Developer > Log Settings > Enabled => Yes

The mode can be enabled by using the command line (only in Magento 2):

For example : for M 2: MAGE_MODE should be set to developer value:

Listen 1900
<VirtualHost 127.0.0.1:1900>
           ServerName example.com
           DocumentRoot ".../magento2"
           SetEnv MAGE_MODE "developer"
           SetEnv MAGE_RUN_TYPE "website"
           SetEnv MAGE_RUN_CODE "mage2"
           RemoteIPHeader X-Forwarded-For
</VirtualHost>

E.g.: Use the command line:

> bin/magento deploy:mode:set developer
############################################
## overrides deployment configuration mode value
## use command bin/magento deploy:mode:set to switch modes
 
# SetEnv MAGE_MODE developer
 
############################################
## uncomment these lines for CGI mode
## make sure to specify the correct cgi php binary file name
## it might be /cgi-bin/php-cgi
 
# Action php5-cgi /cgi-bin/php5-cgi
# AddHandler php5-cgi .php

How to Redirect 301 Using Htaccess in Magento

There are some reasons to redirect 301 using htaccess in Magento including:

  • Changed the store altogether.
  • Changed the URLs as a part of your efforts to improve SEO.
  • Changes in the products you offer
  • Simplified a URL or expanded it to include the details
  • Correct a typo mistake in the URL
  • Deleted a product attribute

If you don’t ensure to redirect 301 results in  “404 – Page Not Found” error. This is the last thing you may want in your stores. So, you need to ensure to redirect 301 using Htaccess in Magento stores.

When redirecting an individual file like example.com/oldfile.htm run a 301 redirect as the following:

Locate the htaccess configuration file in the root on web servers running the Apache Web Server software and add:

Redirect 301 [old URL] [New URL]

For example:

Redirect 301/blog/exception-printing-is-disabled-by-default-for-security-reasons//blog/exception-printing-is-disabled-default-for-security-reasons/

When you use this method, the users and search engines will get redirected to your new URL. So, you can get rid of 404 errors in your Magento stores using redirect 301!

Wrapping Up

We complete the Magento htaccess file today. Should you have any queries, let us know. We have experts who know SEO ins and outs, so we are all ears to solve your problems with SEO optimizations. If you are still getting the error or have any questions, then contact our support team to get an instant solution.  We’re very pleased to support you.

268 Total Views 1 Views Today