Linux 使用 .htaccess 重定向 403 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5444359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
redirect 403 error using .htaccess
提问by X10nD
I want to redirect any 403 using .htaccess, but it does not seem to work.
我想使用 .htaccess 重定向任何 403,但它似乎不起作用。
Options +FollowSymlinks
RewriteEngine on
ErrorDocument 403 notfound.html
RewriteRule notfound.html
All help appreciated.
所有帮助表示赞赏。
Thanks Jean
谢谢让
回答by John Flatness
The URL part of an ErrorDocument directive should either start with a leading slash, which indicates a path that's relative to your DocumentRoot, or it should be a full URL (if you want to use an external document).
ErrorDocument 指令的 URL 部分应该以前导斜杠开头,表示相对于您的 DocumentRoot 的路径,或者它应该是完整的 URL(如果您想使用外部文档)。
You shouldn't need the RewriteEngine and RewriteRule directives at all here.
您在这里根本不需要 RewriteEngine 和 RewriteRule 指令。
So, assuming your notfound.html is at the root level of your site, your directive should look like:
因此,假设您的 notfound.html 位于站点的根级别,您的指令应如下所示:
ErrorDocument 403 /notfound.html
回答by youcantryreachingme
I found a problem with the earlier example, which was:
我在前面的例子中发现了一个问题,那就是:
ErrorDocument 403 /notfound.html
I have a directory on my site storing images, say at domain.com/images
我的网站上有一个存储图像的目录,比如在 domain.com/images
I wanted that if someone tries accessing the directory's URL that they be redirected to the site's homepage. My problem was, with the above, that the URL in the browser would remain domain.com/images and then the homepage would load, but the homepage would reference stylesheets that it could not access from that URL. (And it makes sense - 403 is supposed to show an error message).
我希望如果有人尝试访问目录的 URL,他们将被重定向到该站点的主页。我的问题是,在上述情况下,浏览器中的 URL 将保留为 domain.com/images,然后主页将加载,但主页将引用它无法从该 URL 访问的样式表。(这是有道理的 - 403 应该显示错误消息)。
I then tried researching how to redirect a 403 error to another URL and a few sites said you can't do a 302 or 301 - you're missing the point: 403 is an error, not a redirect.
然后我尝试研究如何将 403 错误重定向到另一个 URL,一些网站说你不能执行 302 或 301 - 你错过了重点:403 是一个错误,而不是重定向。
But I found a way. This is the .htaccess file inside the /images directory:
但我找到了办法。这是 /images 目录中的 .htaccess 文件:
Options -Indexes
ErrorDocument 403 /images/403.php
RewriteEngine on
RewriteRule ^403.php$ /index.php [R=301]
So basically the first line prevents directory listing.
所以基本上第一行防止目录列表。
The second line says that for "permission denied" (ie. 403 error, which is what the first line gives), the site should load the file "/images/403.php". But you don't actually need to create any 403.php file in that directory, because...
第二行表示对于“权限被拒绝”(即第一行给出的 403 错误),站点应加载文件“/images/403.php”。但是您实际上不需要在该目录中创建任何 403.php 文件,因为...
The third line turns on your redirect engine
第三行打开你的重定向引擎
And the fourth line says that if the user attempts to load the page 403.php (ie. in this "/images" directory where this .htaccess file is located) then they should be redirected, using a 301 (permanent) redirect, to /index.php - which is the index of the root directory (ie the homepage), and not the index of the /images subdirectory (and indeed there is no index in "/images" anyway).
第四行表示,如果用户尝试加载页面 403.php(即在此 .htaccess 文件所在的“/images”目录中),则应使用 301(永久)重定向将它们重定向到/index.php - 这是根目录(即主页)的索引,而不是 /images 子目录的索引(实际上,“/images”中没有索引)。
Hence - how to get a 403 to redirect to another URL using a 301 (permanent) redirect (or, you could try a 302 temporary redirect too).
因此 - 如何使用 301(永久)重定向(或者,您也可以尝试 302 临时重定向)让 403 重定向到另一个 URL。
回答by Lessandro
Here is a super easy way to do this:
这是一个超级简单的方法来做到这一点:
Use this line
使用这条线
Options -Indexes
ErrorDocument 403 /goaway.html
where goaway.htmlcontains the following line in the head section:
其中goaway.html在 head 部分包含以下行:
<meta HTTP-EQUIV="REFRESH" content="0; url=HERE YOU PUT THE URL YOU ARE REDIRECTING TO" />
And that's it... each 403 error will go to your goaway.htmlfile in root directory and goaway.htmlwill redirect them to the URL of your choice
就是这样......每个 403 错误都会转到根目录中的goaway.html文件,goaway.html会将它们重定向到您选择的 URL
回答by Thomas Tony
RewriteEngine On
ErrorDocument 403 http://www.yourdomain.com/