使用htaccess的,以掩盖文件夹名称文件夹、名称、htaccess

2023-09-02 00:28:13 作者:社会硬茬子

这里有一个问题,我一直在想解决与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.

让我们说我有一个文件夹在我的网站称为根 /富/ 。我希望用户能够访问该文件夹的路径 /酒吧/ ,但由于种种原因,我不能重命名文件夹。

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.

以免造成混乱我只想要一个路径,永远不会看到 - 这就是说,我的不的希望人们使用的名称 /富/ 来访问该文件夹;他们应该始终使用 /酒吧/ 。如果有人去 example.com/foo / ,浏览器应重定向到 example.com/bar / - 但返回的内容应该是内容 /富/

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

为了使问题更加复杂,在 /富/ 页面有相关性(图片,样式表,链接到其他页面,等等)在 /富/ 这是难于codeD和不能改变。这些必须的,当然,仍能正常工作。

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

这可能吗?它看起来表面上好像它会创建一个无限重定向......但我pretty的肯定有办法prevent,在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 - 一点点额外的背景:正常的原因,我想这样做是重命名字preSS /可湿性粉剂管理员/ 目录的东西更专业,更便于客户记忆,如 /管理/ ,但在同一个系统应该以这种方式掩盖任何路径。)

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

首先,我创建了从 /条一个符号链接 /富/ 。 然后,我在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 / 显示的内容 /富/ 目录, 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的解决方案,我更preFER了!

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