HTACCESS 404错误无法正常工作 [英] HTACCESS 404 error not working properly

查看:67
本文介绍了HTACCESS 404错误无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

在此服务器上找不到请求的URL/welcome.php.
此外,尝试使用ErrorDocument处理请求时遇到404未找到错误.

在这里:

  • 当我访问www.mysite.com/page时,它工作正常,如果我访问mysite.com/404,则它也可以工作

  • 但是,当我拼错某些内容并转到www.mysite.com/pag(而不是"page")时,会出现上述错误.

我还在索引文件(welcome.php)中使用PHP来确定$_GET值并动态显示正确的页面.这可能与此有关.

测试站点: http://mrobertsdesign.ca/home (您可以在 http://mrobertsdesign.ca/hom )

我到处都在寻找答案,在过去的12个小时中,一直在努力寻找答案的原因.最重要的是,我不确定导致问题的原因...感谢所有帮助!

任何想法?


我的.htaccess文件是:

<Files .htaccess>
order allow,deny
deny from all
</Files>

ErrorDocument 404 http://mrobertsdesign.ca/404

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteCond %{HTTP_HOST} ^www\.mrobertsdesign\.ca [NC]
RewriteRule ^(.*)$ http://mrobertsdesign.ca/$1 [R=301]

RewriteRule ^([a-zA-Z0-9]+)/?$ welcome.php?page=$1 [NC]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ welcome.php?category=$1&page=$2 [NC]

DirectoryIndex comingsoon.php welcome.php


我的索引文件是(welcome.php):

<?php 

    $web_url = "http://mrobertsdesign.ca";

    $path = '';
    $page = $_GET['page'];
    $pages = array('home','about','work','contact','404');

    if ((basename($_SERVER['__FILE__'])) == 'welcome.php'){
        if (!empty($page)) {
            $url = '/' . $page;
        } else {
            $page = 'home';
            $url = '/' . $page;
        }
        header('Location: ' . $url, true, 301); exit();
    }
    if (!empty($page)) {
        if(in_array($page,$pages)) {
            if (!empty($category)) {
                $path = $category . '/' . $category . '_' . $page;
            } else {
                $path = 'content_' . $page;
            }
        }else {
            $page = '404';
            $url = '/' . $page;
            header('Location: ' . $url, true, 404); exit();
        }
    }
    else {
        $page = 'home';
        $path = 'content_' . $page;
    }

              if($page == 'home'){
                    // stuff for home here
        }else if($page == 'about'){ 
                    $page_title = "About"; 
        }else if($page == 'work'){ 
                    $page_title = "Work"; 
        }else if($page == 'contact'){ 
                    $page_title = "Contact"; 
        }else if($page == '404'){ 
                    $page_title = "Oops! (404)"; 
        }
        $page_class = 'page-' . $page;

    // Page Start

    include('includes/header.php');
    include('includes/' . $path . '.php');
    include('includes/footer.php');
?>

更改时:

    else {
        $page = 'home';
        $url = '/' . $page;
    }

对此:

    else {
        http_response_code(404); exit; 
    }

该错误更改为welcome.php& amp;的内部服务器错误. 404错误页面:

内部服务器错误

服务器遇到内部错误或配置错误,无法完成您的请求.
请与服务器管理员cgiadmin@yourhostingaccount.com联系,并通知他们错误发生的时间以及您可能做的任何可能导致错误的事情.
服务器错误日志中可能提供有关此错误的更多信息.
此外,尝试使用ErrorDocument处理请求时遇到了500 Internal Server Error错误.


我观察到的更多信息:

  • 我的.htaccess文件正在将诸如/welcome.php?page=about/about以及/welcome.php?category=about&page=me/about/me的值重写,因此,如果我转到/about,它将进入About页面(并且有暂时没有任何/about/me或任何第二层的页面).

  • 如果我拼错了密码并转到/abou/anything-here/anything-here/anything-here-too,则会显示错误,提示它找不到我的索引(welcome.php)和404错误页面. /p>

  • 但是,如果我转到/welcome.php?page=abo(而不是about,不是真实页面的任何内容),它将显示我的404错误页面.

  • 如果我转到/any-page.fileextention(例如/about.php-实际上不存在),它将显示404错误页面,如果我转到/one/two/three,它还将显示404错误页面错误页面.

  • 如果转到/welcome.php(我的索引页面),它将显示正确的页面,并且如果您删除部分文件扩展名(如/welcome.ph),它将显示404错误页面.如果转到404错误页面,它也会正确显示在/404.

当将文件视为文件系统(例如,与/about.php/welcome.php?page=about/welcome.php?page=abo相比,与/about.php相比)时,似乎会引发该错误. >和/about/me).

/welcome.php?page=mypage中,该地址实际上已经指向/welcome.php,但是在/about中,该地址只是.htaccess文件应解释为我的/welcome.php$_GET值的文件系统.页面,当$_GET值为真实值时可以使用,但是当$_GET值为实际值时可以使用,它会作为错误返回并抛出所有错误.

此时的问题甚至不是错误文档,原因是当将地址读取为具有错误的$_GET值的文件系统时,它找不到/welcome.php/404.试图找到/abou/welcome.php,因为存在文件扩展名,并且最终将其扔到真正的404页面,并且它没有尝试进入/abou/404,因为在我的.htaccess文件中,我提供了404页面的完整路径在http://mrobertsdesign.ca/404.这是我迷路的地方.

  • stackoverflow如何实现.htaccess文件的作用,您可以在其中删除部分网址,并尝试用最接近的类似网址填充该网址或抛出404错误页面?

答案!

@Phil_1984_所述,以下行与ErrorDocument请求冲突,这导致我的/welcome.php页面和ErrorDocument请求本身出现404 not found错误:

    else {
        $page = 'home';
        $url = '/' . $page;
    }

他最初的想法是将行更改为以下内容:

    else {
        http_response_code(404); exit; 
    }

这让我开始思考,我已经有了动态布局设置,我只需要将$_GET值返回到页面即可,而不是尝试将整个页面重定向到404页面-事实证明是导致该问题的原因.问题.

解决方案是,不要尝试将整个页面(由于整个站点都是由此PHP脚本和一系列包含内容组成的,因此将其重定向到自身)到404页面,而是将返回到$path,以便包含文件include('includes/' . $path . '.php');可以读取它,并将404文档仅包含到页面中(因为它将以任何方式进行,唯一的区别是URL不变).

    else {
        $page = '404';
        $path = 'content_' . $page;
    }

解决方案

答案!

@Phil_1984_所述,以下行与ErrorDocument请求冲突,这导致我的/welcome.php页面和ErrorDocument请求本身出现404 not found错误:

    else {
        $page = 'home';
        $url = '/' . $page;
    }

他最初的想法是将行更改为以下内容,这对我不起作用(通过500错误):

    else {
        http_response_code(404); exit; 
    }

这让我开始思考,我已经有了动态布局设置,我可以将$_GET值返回到页面,而不是尝试将整个页面重定向到404页面-事实证明是导致该问题的原因.问题.

解决方案是,不要尝试将整个页面(由于整个站点都是由该PHP脚本和一系列包含内容组成的,因此将其重定向到自身)到404页面,而是将返回到$path,以便包含文件include('includes/' . $path . '.php');可以读取它,并将404文档仅包含到页面中(因为它将以任何方式进行,唯一的区别是URL不变).

    else {
        $page = '404';
        $path = 'content_' . $page;
    }

The problem:

The requested URL /welcome.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Here it goes:

  • When I go to www.mysite.com/page it works fine, it also works if I go to mysite.com/404

  • But, when I misspell something and go to www.mysite.com/pag (instead of 'page') I'll get the above error.

I'm also using PHP in my index file (welcome.php) to determine the $_GET value and dynamically display the correct page. It may have something to do with this.

Test site: http://mrobertsdesign.ca/home (you can see the error at http://mrobertsdesign.ca/hom)

I've looked everywhere for answers, been at this for the past 12 hours just trying to figure out why it isn't working. The bottom line is that I'm not sure what's causing the issue... All help is appreciated!

Any ideas??


My .htaccess file is:

<Files .htaccess>
order allow,deny
deny from all
</Files>

ErrorDocument 404 http://mrobertsdesign.ca/404

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteCond %{HTTP_HOST} ^www\.mrobertsdesign\.ca [NC]
RewriteRule ^(.*)$ http://mrobertsdesign.ca/$1 [R=301]

RewriteRule ^([a-zA-Z0-9]+)/?$ welcome.php?page=$1 [NC]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ welcome.php?category=$1&page=$2 [NC]

DirectoryIndex comingsoon.php welcome.php


My index file is (welcome.php):

<?php 

    $web_url = "http://mrobertsdesign.ca";

    $path = '';
    $page = $_GET['page'];
    $pages = array('home','about','work','contact','404');

    if ((basename($_SERVER['__FILE__'])) == 'welcome.php'){
        if (!empty($page)) {
            $url = '/' . $page;
        } else {
            $page = 'home';
            $url = '/' . $page;
        }
        header('Location: ' . $url, true, 301); exit();
    }
    if (!empty($page)) {
        if(in_array($page,$pages)) {
            if (!empty($category)) {
                $path = $category . '/' . $category . '_' . $page;
            } else {
                $path = 'content_' . $page;
            }
        }else {
            $page = '404';
            $url = '/' . $page;
            header('Location: ' . $url, true, 404); exit();
        }
    }
    else {
        $page = 'home';
        $path = 'content_' . $page;
    }

              if($page == 'home'){
                    // stuff for home here
        }else if($page == 'about'){ 
                    $page_title = "About"; 
        }else if($page == 'work'){ 
                    $page_title = "Work"; 
        }else if($page == 'contact'){ 
                    $page_title = "Contact"; 
        }else if($page == '404'){ 
                    $page_title = "Oops! (404)"; 
        }
        $page_class = 'page-' . $page;

    // Page Start

    include('includes/header.php');
    include('includes/' . $path . '.php');
    include('includes/footer.php');
?>

When changing:

    else {
        $page = 'home';
        $url = '/' . $page;
    }

To this:

    else {
        http_response_code(404); exit; 
    }

The error changes to an internal server error for both welcome.php & the 404 error page:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, cgiadmin@yourhostingaccount.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.


More info that I've observed:

  • My .htaccess file is rewriting values such as /welcome.php?page=about to /about and /welcome.php?category=about&page=me to /about/me, so if I go to /about it will go to the about page (and there is no /about/me or any second level of pages at all right now any ways).

  • If I misspell that and go to /abou or /anything-here or /anything-here/anything-here-too it displays the error saying that it can't find both my index (welcome.php) and my 404 error page.

  • However, if I go to /welcome.php?page=abo (instead of about, anything that isn't a real page) it displays my 404 error page.

  • If I go to /any-page.fileextention (eg. /about.php - which doesn't actually exist) it will display the 404 error page, also if I go to /one/two/three it will also display the 404 error page.

  • If you go to /welcome.php (my index page) it will show the correct page, and if you take out part of the file extension like /welcome.ph it will display the 404 error page. If you go to the 404 error page it also displays correctly at /404.

The error seems to be fired when a file is being treated as a file system (eg. /about compared to /about.php or /welcome.php?page=about or /welcome.php?page=abo) for up to two levels in (eg. /about and /about/me).

In /welcome.php?page=mypage, the address literally already points to /welcome.php, but in /about the address is just a file system that the .htaccess file is supposed to interpret as the $_GET value for my /welcome.php page, which works when the $_GET value is a real value, but when it's not it comes back as an error and throws everything off.

The issue isn't even the error document at this point, it's that it can't find /welcome.php and /404 when the address is read as a file system with incorrect $_GET values... It's not trying to find /abou/welcome.php because there is a file extension and would be ultimately thrown to the real 404 page, and it's not trying to go to /abou/404 because in my .htaccess file I've provided the full path to my 404 page at http://mrobertsdesign.ca/404. This is where I'm lost.

  • How does stackoverflow achieve what it's doing with it's .htaccess file where you can delete part of the url and it will try to either fill it in with the closest resembling url or throw a 404 error page?

The answer!

As @Phil_1984_ stated, the following line was conflicting with the ErrorDocument request, which caused the 404 not found error for my /welcome.php page and ErrorDocument request itself:

    else {
        $page = 'home';
        $url = '/' . $page;
    }

His initial idea was to change the line to the following:

    else {
        http_response_code(404); exit; 
    }

Which got me thinking, I already have the dynamic layout setup, I can just return the $_GET value to the page instead of trying to redirect the whole page to the 404 page - which it turns out is what was causing the issue.

The solution was, instead of trying to redirect the whole page (technically back on itself since the whole site is built up of this PHP script and a series of includes) to the 404 page, to just turn the value of $_GET back to $path so that the include file include('includes/' . $path . '.php'); would read it and just include the 404 document into the page (as it would be doing any ways, only difference is that the url doesn't change).

    else {
        $page = '404';
        $path = 'content_' . $page;
    }

解决方案

The answer!

As @Phil_1984_ stated, the following line was conflicting with the ErrorDocument request, which caused the 404 not found error for my /welcome.php page and ErrorDocument request itself:

    else {
        $page = 'home';
        $url = '/' . $page;
    }

His initial idea was to change the line to the following, which didn't work for me (through a 500 error):

    else {
        http_response_code(404); exit; 
    }

Which got me thinking, I already have the dynamic layout setup, I can just return the $_GET value to the page instead of trying to redirect the whole page to the 404 page - which it turns out is what was causing the issue.

The solution was, instead of trying to redirect the whole page (technically back on itself since the whole site is built up of this PHP script and a series of includes) to the 404 page, to just turn the value of $_GET back to $path so that the include file include('includes/' . $path . '.php'); would read it and just include the 404 document into the page (as it would be doing any ways, only difference is that the url doesn't change).

    else {
        $page = '404';
        $path = 'content_' . $page;
    }

这篇关于HTACCESS 404错误无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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