How to disable people from accessing www.mywebsite.com/public/index.php ?

Posted 4 months ago by linesofcode

Hello,

My website is running perfectly in www.mywebsite.com, but I notice people can enter in the URL:

  • www.mywebsite.com/public
  • www.mywebsite.com/public/index.php
  • www.mywebsite.com/server.php

And all links shows my website but without CSS applied. Is there any way I can redirect users to my main URL?

Btw, I use the /public/ folder for JS, CSS & Images, like such:

  • www.mywebsite.com/public/img/somefile.png
  • www.mywebsite.com/public/js/somefile.js
  • www.mywebsite.com/public/css/somefile.css

And I want to maintain that.

This is my root .htaccess:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Remove www    
    RewriteCond %{HTTP_HOST} ^www.mywebsite.com$ [NC]
    RewriteRule ^(.*)$ https://mywebsite.com [R=301,L]

    # Remove http and force https   
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [QSA,R,L]

    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ ^ [N]

    RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
    RewriteRule ^(.*)$ public/ 

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ server.php

</IfModule>

And this is my public/.htaccess:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Finally, I'm using a shared hosting.

Thanks.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.