Security and SSL

Restrict Magento admin access to allowed IPs

Reviewed on 4 min read security access control Magento

Limiting the Magento admin panel to the IPs you work from protects the shop even if someone obtains an administrator password, and stops attacks against flaws in the admin area. It all takes a few lines in the .htaccess.

Open the .htaccess in the folder where Magento is installed from the cPanel File Manager (or by FTP), make a copy and add the rules at the beginning of the file, before Magento's own rules.

Allow a single IP

Find out your public IP (search Google for "what is my IP") and add:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(index\.php/)?admin(/|$) [NC]
RewriteCond %{REMOTE_ADDR} !^203\.0\.113\.10$
RewriteRule ^ - [F,L]

Replace 203\.0\.113\.10 with your IP. Put a backslash \ before each dot and keep the ^ at the start and the $ at the end. Anyone who goes to /admin from another IP will get a 403 error.

Allow several IPs

Add one RewriteCond %{REMOTE_ADDR} line for each IP:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(index\.php/)?admin(/|$) [NC]
RewriteCond %{REMOTE_ADDR} !^203\.0\.113\.10$
RewriteCond %{REMOTE_ADDR} !^203\.0\.113\.12$
RewriteCond %{REMOTE_ADDR} !^198\.51\.100\.25$
RewriteRule ^ - [F,L]

If your provider always gives you an IP from the same range, you can allow the whole range by removing the end and the $. For example, !^203\.0\.113\. allows all IPs starting with 203.0.113.. The shorter the prefix, the more unrelated IPs are allowed.

If your admin path is not /admin

It has usually been changed (Magento 2 suggests a random path when it is installed, such as admin_1a2b3c). You can look it up over SSH from the Magento folder:

php bin/magento info:adminuri

Then replace admin with it in the first condition. For example, for backoffice:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(index\.php/)?backoffice(/|$) [NC]
RewriteCond %{REMOTE_ADDR} !^203\.0\.113\.10$
RewriteRule ^ - [F,L]

If the shop is in a subfolder (for example tudominio.com/tienda), include that folder: ^/tienda/(index\.php/)?admin(/|$).

If your IP changes every day: allow only your country

With a dynamic IP connection you cannot fix an IP. A less strict alternative is to allow the panel only from your country. The server geolocates every visit, so all you need is:

GeoIPEnable On
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(index\.php/)?admin(/|$) [NC]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} !^(ES)$
RewriteRule ^ - [F,L]

ES is Spain. For several countries, separate them with |: !^(ES|PT)$. You will find the other codes and more examples in block or allow access by country.

Check that it works

  1. From your connection, go to the panel: it should load as usual.
  2. From your phone on mobile data (another IP), go to the same address: it should return a 403 "Forbidden" error.
  3. Also try the variant with index.php (tudominio.com/index.php/admin): it should return the same 403.

If you lock yourself out, edit the .htaccess from the cPanel File Manager and correct the IP or remove the block. If the shop is behind Cloudflare or another proxy, the server may see the proxy's IP rather than yours: in that case set up the restriction in Cloudflare itself.

You may also find useful

Something wrong or out of date? Tell us and we will review it.