重写Url以隐藏文件夹名称,并在没有文件夹名称的情况下打开它


Url rewriting for hiding folder name and open it without folder name

我已经在根目录中使用了我所有的uls,现在我无法更改所有它们,所以我需要重写隐藏文件夹名称的主url。如果有人打开了该域,则保存在该文件夹中的数据也必须打开,但文件夹名称不应显示在url中,即我有这个

http://example.com/foldername

.我想把这个改成

http://example.com

但它应该自动获取文件夹名数据。如何实现这一点通过使用".htaccess"文件

RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# The following rewrites all other queries to index.php. The 
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to 
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size 
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::'2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

这是我当前使用的.htaccess文件

将此规则保留在根目录中。htaccess:

RewriteEngine On
RewriteRule ^(?!foldername/)(.*)$ foldername/$1 [L,NC]

然后在/fodlername/.htaccess:中有这个简化的代码

DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

像这样更改.htaccess文件

RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET' /foldername/
RewriteCond %{HTTP_HOST} ^(www'.)?yoursite.com$
RewriteRule ^foldername/(.*) /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www'.)?yoursite.com$
RewriteRule !^foldername/ foldername%{REQUEST_URI} [L]
相关文章: