.htaccess:身份验证和重定向

发布于 2025-01-06 02:37:48 字数 486 浏览 4 评论 0原文

我正在尝试使用 .htaccess 将我的一个域重定向到另一个域:

RewriteEngine on
RewriteRule !^admin($|/) http://mydomain.com%{REQUEST_URI} [L,R=301]

如您所见,我不想重定向给定文件夹(“admin”),因为该目录实际上有内容。

问题是我在该目录中有另一个 .htaccess ,其中包含以下代码:

AuthName "You must login"
AuthUserFile /path/to/.htpasswd
AuthType Basic
require valid-user

当我访问浏览器中的非重定向文件夹时,它会重定向到 mydomain.com 上的 401.html 页面。

我该如何解决这个问题?我想将所有内容重定向到 mydomain.com,除了需要身份验证的目录“admin”。

I'm trying to redirect one of my domains to another one using .htaccess:

RewriteEngine on
RewriteRule !^admin($|/) http://mydomain.com%{REQUEST_URI} [L,R=301]

As you can see, I don't want to redirect the given folder ("admin") because that directory actually has content.

The problem is that I have another .htaccess in that directory with this code:

AuthName "You must login"
AuthUserFile /path/to/.htpasswd
AuthType Basic
require valid-user

When I visit the non-redirecting folder in my browser, it redirects to a 401.html page on mydomain.com.

How do I fix this? I want to redirect everything to mydomain.com, except the directory "admin" which requires auth.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

·深蓝 2025-01-13 02:37:48

在您的第一个域的配置文件中查找 R=401ErrorDocument 401。设置自定义 401 页面,然后该页面与您的重写规则相匹配,并将浏览器重定向到 mydomain.com

Look for a R=401 or a ErrorDocument 401 in the config files for your first domain. Something is setting a custom 401 page that is then matching against your rewrite rule and it's redirecting the browser to mydomain.com.

提笔落墨 2025-01-13 02:37:48

对于粗心的人来说,一个陷阱是您的浏览器可能在测试期间缓存您的重定向。因此,在测试时,请从 R=301 中删除 301,以保留 R 标志。

因此:

RewriteRule !^admin($|/) http://example.com%{REQUEST_URI} [L,R]

一旦您对 htaccess 重定向的工作感到满意,请将 R 标志更改为 R=301(作为永久重定向)。

如果您一直使用 R=301,请清除浏览器的缓存。

A trap for the unwary is that your browser is probably caching your redirects during testing. So when testing remove the 301 from R=301 to leave the R flag by itself.

Thus:

RewriteRule !^admin($|/) http://example.com%{REQUEST_URI} [L,R]

Once you are happy with the workings of your htaccess redirect then change the R flag to R=301 (being a permanent redirect).

If you have been using R=301, then clear your browser's cache.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文