0

I have following requirements which i believe can be accomplished using .htaccess file. Requirements-

  1. Hide Folder names and file extension in URL Eg. www.example.com/subfolder/subfolder1/file.php should become www.example.com/file

  2. Restrict Folder browsing - I want to restrict folder browsing capability when somebody fires an URL eg. www.example.com/subfolder Conventionally by firing this URL user will be able to browse through the contents of subfolder. By firing such URL or any URL containing domain example.com eg. www.example.com/folderNotExist then server should redirect to index page.

I am able to restrict folder browsing but redirection to index page and hiding of folder and file extension is not working.

2
  • please add the rules you've tried that don't work Commented Jun 23, 2016 at 4:35
  • 'stackoverflow.com/questions/30366605/…' This is the page i took help from. I do not have the exact script right now. Since it did not worked i deleted it. Commented Jun 23, 2016 at 4:51

3 Answers 3

1

You can have these rules in your root .htaccess:

ErrorDocument 404 / DirectoryIndex index.php RewriteEngine On RewriteCond %{THE_REQUEST} /subfolder/subfolder1/ [NC] RewriteRule ^ / [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{DOCUMENT_ROOT}/subfolder/subfolder1/$1.php -f RewriteRule ^(.+?)/?$ subfolder/subfolder1/$1.php [L] 
Sign up to request clarification or add additional context in comments.

Comments

0

You should try something like:

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*?)$ /subfolder/subfolder1/$1.php ErrorDocument 404 /index.php 

2 Comments

It is NOT working. I have put my .htaccess file at root location i.e.e where my index.php resides. Infact what it does is that it restricts access to index.php aswell unless i type localhost/dashboard/index.php It is then neither satisfying requirement 1 as well as requirement 2
@VishalKhare you will need to clear your browser history and then force refresh the page to active the updated .htaccess file.
0

you can try this

RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^www.mysample.com RewriteRule ^subdir/(.*)$ http://www.mysample.com/$1 [L,R=301] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.