5

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.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Igor
  • 645
  • 4
  • 16
  • 37

1 Answers1

2

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • when entering `meusite.com.br/users/teste` remains the same, and at the access `meusite.com.br/teste` he rewrites for `meusite.com.br/users/teste` and I need the exact opposite. If you access `meusite.com.br/users/teste` rewrite to `meusite.com.br/teste`. Thank you bro and sorry again for my bad english. – Igor May 01 '15 at 02:10
  • If I access `meusite.com.br/users/teste` it rewrites to `meusite.com.br/teste` correctly but nothing appears and the following message occurs *"This web page has a redirect loop."* And if I access `meusite.com.br/teste` he rewrites for `meusite.com.br/users/teste` and displays the same message. (in this case should not rewrite). – Igor May 01 '15 at 02:22
  • 1
    Check another update. You should enter `meusite.com.br/teste/` with a trailing slash since `teste` is a directory. It will internally open `meusite.com.br/users/teste/` – anubhava May 01 '15 at 02:25
  • It works, but I get an error 403 and the *index.html* and that is within `teste` is not displayed. – Igor May 01 '15 at 02:31
  • The error occurred because I'm using `Options All -Indexes` and by removing appears the `index of users/teste`. The `index.html` appears there, but it is as if there were. – Igor May 01 '15 at 02:40
  • 1
    Another minor update in `# add a trailing slash to directories` rule. It should add a trailing `/` and make it `meusite.com.br/teste/`. If that doesn't work then try: `Options All` – anubhava May 01 '15 at 02:58
  • It remains the same, what to do? – Igor May 01 '15 at 03:05
  • Finally, it worked! But, there was another problem: the code below the `# Hide GET variables link.` stopped working. *(Returning a error 404, it was not happening before.)* – Igor May 01 '15 at 03:12
  • 2
    @Igor change `RewriteRule ^/?((?!users/).*)$` to `RewriteRule ^((?!users/|posts/).*)$` – hjpotter92 May 01 '15 at 04:05