如果未使用.htaccess登录,如何重定向到特定页面 [英] How to redirect to specific page if not logged in with .htaccess

查看:97
本文介绍了如果未使用.htaccess登录,如何重定向到特定页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows PC上运行apache2和php5.

I am running apache2 and php5 in my windows PC.

我已经使用.htaccess.htpasswd保护了我的目录.如果未设置登录信息,或者用户名和密码组合不正确,则默认情况下,如果用户尝试访问受保护的目录,浏览器将提示输入用户名和密码框.但是我想将用户重定向到特定的地址或URL.简而言之,我想重定向用户而不是显示HTTP基本身份验证对话框.我怎样才能做到这一点?

I have protected my directory using .htaccess and.htpasswd. If login information is not set, or if the username-password combination is not correct, the browser will prompt for a username and password box by default, if user tries to access protected dir. But I want to redirect the user to a specific address or url. In short, I want to redirect user instead of displaying the HTTP basic authentication dialog. How can I make this possible??

提前感谢... :)

Thanks in advance... :)

blasteralfred

blasteralfred

推荐答案

我将其与类似AJ的方法一起使用.我的.htaccess文件与以下内容非常相似:

I got this to work with an approach similar to AJ's. My .htaccess file is very similar to the following:

AuthUserFile /opt/www/htaccess
AuthType Basic

DirectoryIndex public.txt

<Files "secret.txt">
    require valid-user
    FileETag None
    Header unset ETag
    Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</Files>

<Files "public.txt">
    FileETag None
    Header unset ETag
    Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</Files>

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP:Authorization} !=""
RewriteRule ^$ secret.txt [L]

以此,网站的行为如下:

With this, the site behaves as follows:

1)访问基本URL->查看public.txt中的内容. 2)访问/secret.txt->提示进行身份验证,并显示secret.txt的内容. 3)再次访问基本URL->查看secret.txt中的内容.

1) Access the base URL -> see content from public.txt. 2) Access /secret.txt -> prompted to authenticate, and shown the contents of secret.txt. 3) Access the base URL again -> see content from secret.txt.

使用[L,R]而不是[L]将使用302响应来处理重定向.如果希望重定向在浏览器的位置"字段中可见,这是一个不错的选择.

Using [L,R] instead of [L] will use a 302 response to handle the redirection. This is a good option if you want the redirection to be visible in the browser's location field.

< aside>是的,我意识到这是一个很晚的答案.不过,这个问题在Google搜索结果中还是很高的,因此,我想详细介绍自己的方法,以防将来发现自己进行相同的搜索.如果其他人受益,那就更好了.</aside>

这篇关于如果未使用.htaccess登录,如何重定向到特定页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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