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

时间:2016-05-05 11:56:03

标签: php .htaccess mod-rewrite url-rewriting

我已经在根目录中使用了所有我的uls现在我无法更改所有这些因此我需要重写隐藏文件夹名称的主页URL,如果有人打开该域,那么保存在该文件夹中的数据也必须打开但是文件夹名称不应显示在网址中,即我有这个

http://example.com/foldername

。我希望将其更改为

http://example.com

但它应该自动获取foldername数据。如何实现这一点 使用" .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文件

2 个答案:

答案 0 :(得分:2)

将此规则保留在root .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]

答案 1 :(得分:0)

像这样更改您的.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]