Vishal Khare
Vishal Khare

Reputation: 73

How to hide folder names and file extension in URL?

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.

Upvotes: 0

Views: 1397

Answers (3)

anubhava
anubhava

Reputation: 784898

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]

Upvotes: 1

aniket ashtekar
aniket ashtekar

Reputation: 370

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 

Upvotes: 0

Riad Loukili
Riad Loukili

Reputation: 119

You should try something like:

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

Upvotes: 0

Related Questions