By default, the URL to access the admin dashboard in Magento 2 is: yourmagento2website.com/admin/
If you keep using this default admin URL, anyone can use the password brute force methods to scan for admin credentials and log into your website for the purpose of hacking your Magento 2 website.
So the best way to avoid these spoilers is to change the default admin page login URL.
In this article, Magentip will guide you to do that in 3 ways to change the default admin url in Magento 2:
- Change admin url of Magento 2 in admin dashboard
- Change admin url in env.php file
- Change admin url using Magento 2 command
3 methods work the same way, so choose the method that works best for you.
Note: I also published a tutorial on how to change base URL of Magento 2 here.
Before we start
Please make sure that you have full access to your server so that you can retrieve the admin url even you cannot log in to your website.
Method 1 (easiest): Change Magento 2 admin URL in Admin dashboard
This is the easiest method to change admin url of Magento 2.
Step 1: Login to your Magento 2 admin dashboard and go to Stores > Configuration > Advanced > Admin
Remember to choose the scope you want to change admin URL
Step 2: Expand “Admin Base URL” section and change the following values
Untick Use system value for the value you want to changes.
Use Custom Admin URLchange the option to Yes to enable using custom admin URL
Custom admin URL, enter the new URL you want to change
If you want to use Custom Admin Path, select yes in
Use Custom Admin Path . Custom Admin path is the text after the last forward slash.
For example, if you set
Custom Admin URL = myadmin and
Custom Admin Path = mypath => The final admin URL will be: yourmagento2website.com/myadmin/mypath
Finally, save changes and from now you can access the admin dashboard using your new admin URL.
Method 2: Change Magento 2 admin URL in env.php file
You can also change admin URL using
Step 1: Connect to your server using a file manager program
Step 2: Go to app/etc/env.php and find the line:
'frontName' => 'admin'
Step 3: Change the value
admin to your new desired admin url.
For example, if I want to set new admin URL = mywebsite.com/myadmin
return [ 'backend' => [ 'frontName' => 'myadmin' ],
Finally, save changes to the file and flush cache to apply new admin url
You can use flush cache command:
php bin/magento cache:flush
Or flush cache in System > Cache Management
Now you should be able to access the admin dashboard with the new URL
Method 3: Change Magento 2 admin URL using ssh command
You can also change the admin URL of Magento 2 using Magento 2 command. This method is recommended for users that are familiar with the Magento 2 command interface.
Step 1: Connect to your server using SSH protocol with root access
Step 2: Run this command
php bin/magento setup:config:set --backend-frontname="myadminurl"
myadminurl with your desired admin url.
If you succeed, the following message will be displayed
Note: During URL changing process, there will be some question from system about overwriting configuration, type “Y” to proceed.
Overwrite the existing configuration for backend-frontname?[Y/n]Y Overwrite the existing configuration for db-ssl-verify?[Y/n]Y Overwrite the existing configuration for allow-parallel-generation?[Y/n]Y We saved default values for these options: amqp-host, amqp-port, amqp-user, amqp-password, amqp-virtualhost, amqp-ssl, amqp-ssl-options, db-ssl-key, db-ssl-cert, db-ssl-ca, db-ssl-verify, allow-parallel-generation.
Restore the default Admin URL and Admin Path
In case you lose the admin URL, you can easily revert to the default admin url using this command
Revert Custom Admin URL
php bin/magento config:set admin/url/use_custom 0
This command works like you change option in Use Custom Admin URL field to Use system value
Revert Custom Admin Path
php bin/magento config:set admin/url/use_custom_path 0
Similarly, this command change Use Custom Admin Path to Use system value
Now, you should be able to access admin dashboard using the default admin URL of Magento 2: yourwebsite.com/admin/
It’s crucial to protect your website, especially an eCommerce website like Magento 2 from potential hacking threats. Changing the admin URL is one of the very first steps to protect your website from hackers.
In the next months, I will release more tutorials about improving Magento 2 store’s security. So, dont forget to subscribe to our news to keep updating with the latest Magento 2 tutorials from Magentip.com.
Drop a comment below if you encounter any problem when changing admin URL of Magento 2, I will be happy to help!