How to restrict direct access /pub with the project URL (www.domain.com/pub) in Magento 2.

  • do you have a redirect url for that? I mean when you navigate to www.domain.com/pub, does it show 404 page or not? – Korbin Sep 14 '18 at 7:58
  • It doesn't show 404 error. I get the homepage. I can't set redirect for www.domain.com/pub using htaccess. – Intellect Outsource Sep 14 '18 at 8:37

You should make your web server to point to the pub folder directly.

Assuming the web server root is /var/www/html, and the magento itself is in a folder named yourmagento, in Apache this can be done like

<VirtualHost *:80>
    ServerName yourdomain.com
    DocumentRoot /var/www/html/yourmagento/pub

The chances are that the current path for the DocumentRoot is /var/www/html/yourmagento

Please check with your own server configuration. For nginx the syntax is slightly different, the root folder of the web server can be completely different, but the idea will be the same:

Make the web server to point directly to the /pub folder, not the magento root folder.

And yes, once the configuration is saved, make sure you restart all needed services, and re-deploy static content of Magento.

  • But my project is hosted in shared server. Is this solution applicable in shared hosting? – Intellect Outsource Sep 14 '18 at 9:17
  • No unlikely. But in this case ask your support if they would be able to make the domain point to the /pub folder, i.e. one level deeper than the current one. – Marjan Sep 14 '18 at 9:22

I think you can block the url in robots.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.