Hiding existance of directory and file structure with .htaccess

Viewed 215

I want to hide my website structure and redirect all possible requests to one file (index.php) with .htaccess.

Right now I use .htaccess to deny permission to read images and directories and redirect requests to index.php. If anybody requests forbidden dir/file, it shows E403. I want to redirect the request to index.php in that case, no E403.

So basically: I want to start processing every possible request (existing/non-existing page, .php file, image, other file, dir..., except css and js) in a single file (index.php), no matter what, so the user will see my index.php content every time (generated from many files on server side, depending on URI). My .htaccess can hide image content, but not the structure and PHP files. If anybody requests invalid URI, it's working (index.php shows error message), but if somebody requests for an existing PHP, it's executing it.

Order allow,deny
Deny from all
</Files>

<FilesMatch "\.(log|jpg|png|jpeg|gif|ico|jfif|pdf|phtml)$">
Order allow,deny
Deny from all
</FilesMatch>

Options -Indexes

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(css|js)$ index.php [L]

AddType application/x-httpd-php .php .phtml
1 Answers

You can define specific error page in php or html format. and you can redirect your visitor to specific page

    ErrorDocument 404 /errors/404.php
    ErrorDocument 403 /errors/403.php

For example in 404.php you can use these code :

header("Location: http://example.com/index.php");

or if you wanna use index.htmlyou can do this :

<html>
    <head>
<meta http-equiv="refresh" content="0; url=http://example.com/" />
    </head>
</html>

or also can do something like this :

    ErrorDocument 404 /index.php
    ErrorDocument 403 /index.php

this link is also helpful

and for your new comment you the answer is "Yes". you can process an extension as another extension, take look at this code :

AddType application/x-httpd-php .mp3
RewriteEngine On
RewriteRule /load.mp3 /load.php

this code will add mime-type and will proccess .mp3 files as .php you also can use this code :

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.+).mp3$ /$1.php [NC,L]

and this link is useful too

Related