我试图隐藏目录 users
网址(例如:):meusite.com.br/users/teste
但到目前为止还没有成功。像这样访问 meusite.com.br/teste
显示文件夹内的内容 teste
并且,如果访问 URL meusite.com.br/users/teste
/users/
被移除,只展出了 meusite.com.br/teste
.
我试过了:
RewriteCond %{REQUEST_URI} !^/users
RewriteRule (.*) /users/$1 [QSA,L]
但我没有成功。我也试过:
RewriteCond %{REQUEST_URI} !^/users
RewriteRule ^/?([^/]+)$ /users/$1 [L]
但没有工作。
为了更好地理解,它遵循站点文件夹的部分结构:
├── users
| ├── teste
| | └── index.html
| └── outroteste
| └── index.html
├── .htaccess
└── index.php
因为我的文件“htaccess”已经是:
<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>
我希望你能帮忙。
最佳答案
您可以使用您的 users
隐藏规则就在重定向规则下面:
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]
但请记住,它会将所有内容转发到
/users/
从而使您的最后一条规则失效。
关于.htaccess - 使用 htaccess 隐藏目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29980588/