使用mod_rewrite htaccess隐藏子文件夹

时间:2014-04-13 09:52:06

标签: php apache .htaccess mod-rewrite

如何在两个网址中隐藏项目子文件夹,我尝试了很多方法,但没有任何进展顺利

以下是网址

mydomain.com/project/service.php?param=40&some/text/goes/here

mydomain.com/project/item.php?param=15&some/text/goes/here

这是我到目前为止尝试的代码。

<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews -Indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]

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

非常感谢任何帮助,谢谢!

2 个答案:

答案 0 :(得分:0)

这应该有效:

 RewriteEngine On
 RewriteBase /
 RewriteCond %{REQUEST_URI} ^/project/
 RewriteRule ^project/(.*)$ /$1 [r,L]

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

这将删除URL中的project文件夹,然后以静默方式将其添加回来(用户看不到它被重新读取。

答案 1 :(得分:0)

考虑到你的意见,我已经改写了你的htaccess

Options +FollowSymLinks -MultiViews -Indexes

RewriteEngine on
RewriteBase /

# hide PROJECT folder from url
RewriteCond %{THE_REQUEST} \ /project/(.*)\ HTTP/ [NC]
RewriteRule . %1 [R=301,L]

# hide homepage index.php
RewriteCond %{THE_REQUEST} \ /index\.php [NC]
RewriteRule . / [R=301,L]

# check if file/folder exists in PROJECT folder (if so, internally rewrite)
RewriteCond %{DOCUMENT_ROOT}/project%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/project%{REQUEST_URI} -d
RewriteRule ^(.*)$ project/$1 [L,QSA]

# internally rewrite everything (except existing file/folder) to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]