.htaccess - Hiding a directory in the URL while preserving other files

不羁的心 提交于 2019-12-02 01:30:31
Lars Ebert

You will need something like this:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule    /studio/(.*) /studio/tools/$1    [L]
 </IfModule>

The problem is it will not work if the requested file is both in the studio-folder and the tools-folder.

But there no way to prevent this, as the server never knows if an URL is meant to refer to /studio/tools/ or /studio/

Edit: You can remove /tools/ from the visible urls like this:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule    /studio/(.*) /studio/tools/$1    [L]
    RewriteRule    /studio/tools/(.*) /studio/$1    [L,R=302]
 </IfModule>

In this example there is not 404 but it only works to hide only one dir

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+dirname/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^dirname/)^(.*)$ /dirname/$1 [L,NC]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!