如何使用htaccess从URL中删除子目录名?

时间:2018-01-23 05:49:24

标签: .htaccess

我的网址是

  

http://demo.example.com/parentFolder/

但我必须将其重定向到子文件夹,URL为

  

http://demo.example.com/parentFolder/ChildFolder/

我已将其重定向为htaccess

<IfModule mod_rewrite.c>

       RewriteEngine On
       RewriteCond %{HTTP_HOST} ^(www.)?demo.example.com/parentFolder$
       RewriteCond %{REQUEST_URI} !^/ChildFolder/


       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteCond %{REQUEST_FILENAME} !-d

       RewriteRule ^(.*)$ /ChildFolder/$1

       #RewriteCond %{HTTP_HOST} ^(www.)?demo.example.com/parentFolder$
       RewriteRule ^(/)?$ ChildFolder/ [L]

</IfModule>

但没有从网址中删除子文件夹(ChildFolder)。 我想在重定向后访问childFolder,但想要从URL隐藏childFolder。

1 个答案:

答案 0 :(得分:1)

您可以在 parentFolder / .htaccess 中使用这些规则:

RewriteEngine On

# remove ChildFolder from URL show in browser
RewriteCond %{THE_REQUEST} \s/+(parentFolder)/ChildFolder/(\S*)\s [NC]
RewriteRule ^ /%1/%2 [R=301,NE,L]

# add ChildFolder internally

RewriteRule ^$ ChildFolder/ [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!ChildFolder/).*$ ChildFolder/$0 [L,NC]