.htaccess:身份验证和重定向
我正在尝试使用 .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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的第一个域的配置文件中查找
R=401
或ErrorDocument 401
。设置自定义 401 页面,然后该页面与您的重写规则相匹配,并将浏览器重定向到mydomain.com
。Look for a
R=401
or aErrorDocument 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 tomydomain.com
.对于粗心的人来说,一个陷阱是您的浏览器可能在测试期间缓存您的重定向。因此,在测试时,请从 R=301 中删除 301,以保留 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:
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.