Use htaccess to mask folder name

后端 未结 2 515
梦谈多话
梦谈多话 2020-12-11 13:07

Here\'s a problem I\'m always wanting to solve with htaccess. I haven\'t found a way yet, though it looks like it should be possible - perhaps someone can help.

Let\

相关标签:
2条回答
  • 2020-12-11 13:43

    I found a sort of workaround - by using a symlink and htaccess in combination.

    First I created a symlink from /bar to /foo/.
    Then I put this in htaccess :

    Options +FollowSymLinks
    RewriteRule ^foo/(.*)$ bar/$1 [R,L]
    

    This has exactly the desired result - example.com/bar/ shows the content of the /foo/ directory, and example.com/foo/ redirects to example.com/bar/

    But if anyone can come up with a pure htaccess solution I'd much prefer that!


    Update :

    Ok, I've finally found out how to do this. It turns out to be quite simple...

    RewriteCond %{THE_REQUEST} ^GET\ /foo/
    RewriteRule ^foo/(.*)$ bar/$1 [R,L]
    
    RewriteRule ^bar/(.*)$ foo/$1
    

    The only problem is that it doesn't take account of RewriteBase, so you have to include the full path in the first line (after ^GET\).

    0 讨论(0)
  • 2020-12-11 13:59

    If I understand correctly what you want is something like this inside your .htaccess file:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^foo/$ bar/
    RewriteRule ^bar/$ foo/
    </IfModule>
    
    0 讨论(0)
提交回复
热议问题