简体   繁体   English

.htaccess文件重定向到索引页面,而不是自定义404页面

[英].htaccess file redirecting to the index page instead of a custom 404 page

I have a website where I need to show a custom error page. 我有一个网站,需要显示自定义错误页面。 I have done it before by editing the .htaccess file. 我之前通过编辑.htaccess文件来完成此操作。

This is what I have in my .htaccess file: 这是我的.htaccess文件中的内容:

RewriteEngine On
  RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.*$ [NC] 
 RewriteRule \.(gif|jpg|jpeg)$ http://www.domain.com [L]
 RewriteCond %{HTTP_HOST} !^www\.
   RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
  ErrorDocument 404 /error404.php
   ErrorDocument 403 /error404.php
   Options -Indexes 

      RewriteCond %{THE_REQUEST} \s/+page\.php\?url=([^\s&]+) [NC]
     RewriteRule ^ /%1? [R=301,L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
       RewriteRule ^(.+)$ /page.php?url=$1 [L,QSA]

The page always redirects to the index page instead of showing the error page. 该页面始终重定向到索引页面,而不显示错误页面。 What am I doing wrong? 我究竟做错了什么? I have tried removing many lines, but nothing helped. 我尝试删除许多行,但没有任何帮助。

You can use: 您可以使用:

ErrorDocument 404 /error404.php
ErrorDocument 403 /error404.php
Options -Indexes
RewriteEngine On

RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.*$ [NC] 
RewriteRule \.(gif|jpg|jpeg)$ http://www.domain.com [L]

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{THE_REQUEST} \s/+page\.php\?url=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)/?$ /page.php?url=$1 [L,QSA]

This will load your custom 404 handler if request is for some extension like .php or .html . 如果请求扩展名是.php.html这将加载您的自定义404处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM