Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide a directory with htaccess

I'm trying to hide a directory users of url (ex.:): meusite.com.br/users/teste but so far not succeeded. Like that by accessing meusite.com.br/teste show the content inside the folder teste and, if accessed URL meusite.com.br/users/teste the /users/ were removed and only exhibited the meusite.com.br/teste.

I've tried:

RewriteCond %{REQUEST_URI} !^/users
RewriteRule (.*) /users/$1 [QSA,L]

but I did not succeed. I also tried:

RewriteCond %{REQUEST_URI} !^/users
RewriteRule ^/?([^/]+)$ /users/$1 [L]

but not worked.

For a better understanding, it follows part of the structure of the site folders:

├── users
|   ├── teste
|   |   └── index.html
|   └── outroteste
|       └── index.html
├── .htaccess
└── index.php

As my file 'htaccess` is already:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Rewrites the requested http to https.
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

    # Hide GET variables link.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^posts/(.*) posts.php?p=$1
</IfModule>

I hope you can help.

like image 833
Igor Avatar asked Oct 31 '22 05:10

Igor


1 Answers

You can use your users hiding rule just below redirect rule:

DirectorySlash Off
RewriteEngine On

# Rewrites the requested http to https.
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]

# add a trailing slash to directories
RewriteCond %{DOCUMENT_ROOT}/users/$1/ -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302,NE]

RewriteCond %{THE_REQUEST} /users/(\S*)\s [NC]
RewriteRule ^ /%1 [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?((?!users/).*)$ users/$1 [L,NC]

# Hide GET variables link.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^posts/(.*) posts.php?p=$1 [L,QSA]

But just keep in mind it will forward everything to /users/ thus making your last rule defunct.

like image 176
anubhava Avatar answered Nov 09 '22 10:11

anubhava