使用 htaccess 屏蔽文件夹名称 [英] Use htaccess to mask folder name

查看:24
本文介绍了使用 htaccess 屏蔽文件夹名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我一直想用 htaccess 解决的一个问题.我还没有找到方法,虽然看起来应该是可能的 - 也许有人可以提供帮助.

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.

假设我的站点根目录中有一个名为 /foo/ 的文件夹.我希望用户能够在路径 /bar/ 处访问该文件夹,但由于各种原因我无法重命名该文件夹.

Let's say I have a folder at the root of my site called /foo/. I want users to be able to access that folder at the path /bar/, but for various reasons I can't rename the folder.

为了不造成混淆,我只希望看到一条路径 - 也就是说,我希望人们使用名称 /foo/ 访问文件夹;他们应该总是使用 /bar/.如果有人访问 example.com/foo/,他们的浏览器应该重定向到 example.com/bar/ - 但返回的内容应该是 /foo/.

So as not to create confusion I only want one path to ever be seen - that is to say, I don't want people to use the name /foo/ to access the folder; they should always use /bar/. If someone goes to example.com/foo/, their browser should redirect to example.com/bar/ - but the content returned should be the content of /foo/.

让事情变得更复杂的是,/foo/ 中的页面在 /foo/ 中具有依赖项(图像、样式表、到其他页面的链接等),这些依赖项是硬编码的并且不能改变.当然,这些必须仍然有效.

To make matters more complicated, pages in /foo/ have dependencies (images, stylesheets, links to other pages, etc) within /foo/ which are hardcoded and can't be changed. These must, of course, still work.

总而言之,这就是我想要的:

So, to summarise, this is what I want :

  • example.com/foo/ 的请求应重定向到 example.com/bar/.
  • example.com/bar/ 的请求应返回 example.com/foo/ 的内容.
  • Requests for example.com/foo/ should redirect to example.com/bar/.
  • Requests for example.com/bar/ should return the contents of example.com/foo/.

这可能吗?从表面上看,它似乎会创建无限重定向……但我很确定在 htaccess 中可以防止这种情况发生,不是吗?

Is this possible? It looks on the surface as if it would create an infinite redirect... but I'm pretty sure there are ways to prevent that in htaccess, aren't there?

如果能得到任何帮助,我将不胜感激.

I'd be very grateful for any help.

(PS - 一些额外的背景:我想要这样做的正常原因是将 wordpress /wp-admin/ 目录重命名为更专业且易于客户记住的目录,例如作为 /admin/.但同样的系统应该可以以这种方式屏蔽任何路径.)

(PS - for a little extra background: The normal reason I want to do this is to rename the wordpress /wp-admin/ directory to something more professional and easy for customers to remember, such as /admin/. But the same system should work for masking any path in this way.)

推荐答案

我找到了一种解决方法 - 通过组合使用符号链接 htaccess.

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

首先我创建了一个从 /bar/foo/ 的符号链接.
然后我把它放在 htaccess 中:

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

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

这正是想要的结果 - example.com/bar/ 显示了 /foo/ 目录的内容,而 example.com/foo/ 重定向到 example.com/bar/

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/

但如果有人能想出一个纯粹的 htaccess 解决方案,我更喜欢那个!

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

更新:

好的,我终于找到了如何做到这一点.结果很简单...

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

唯一的问题是它没有考虑RewriteBase,所以你必须在第一行(^GET之后)包含完整路径.

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).

这篇关于使用 htaccess 屏蔽文件夹名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆