简体   繁体   中英

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.

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]

You should try something like:

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

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM