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

前端 未结 2 742
温柔的废话
温柔的废话 2021-01-22 08:22

I developed a giant studio of tools and each tool has its own directory in the /tools/ folder. So if you have a tool named example, the URL would be /studio-dir/tools/e

2条回答
  •  一整个雨季
    2021-01-22 08:58

    You will need something like this:

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

    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:

    
        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]
     
    

提交回复
热议问题