Mod 重写在爬行时产生 404(在浏览器中查看时工作正常)

发布于 2024-09-18 11:15:37 字数 2511 浏览 5 评论 0原文

我的 .htaccess 中有以下代码:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

我的漂亮链接转换是在我的 index.php 上完成的。如果用户正在寻找不存在的内容,则会生成 404 标头,否则将显示内容。

这一切都很好,但是当爬虫或其他东西尝试查看 HTML 时,他们会收到 404 错误。

例如:

http://www.jasonleodurbin.com/portfolio 该链接应该可以正常工作。

如果您尝试在 HTML 验证器中验证它,它不会工作。爬虫说它收到了 404 错误。我在 Facebook 分享中也收到了同样的错误。

我尝试删除 404 错误标头,但仍然遇到同样的问题。

这是什么交易?有什么建议吗?

编辑:

新的.htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^jasonleodurbin\.com$ [NC]
RewriteRule ^(.*)$ http://www.jasonleodurbin.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+)/$ index.php?go=$1&app=1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+)?success=true index.php?go=$1&success=1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+) index.php?go=$1 [NC,L,QSA]

处理:

globals.php

if(!isset($_GET['go']) || $_GET['go'] == "") $_GET['go'] = "home";

header.php

if(isset($_GET['go']) && !@fopen(strtolower($_GET['go']).".php",'r')){
    //header("HTTP/1.0 404 Not Found");
    define("FAIL",true);
    $_GET['go'] = "error";
}
else {
    define("FAIL",false);
Header('HTTP/1.1 200');
}
if(FAIL) define("GOSUB",DIR);
if(isset($_GET['app'])) define("GOSUB","../");
else define("GOSUB","");

我也为index.php尝试过这个:

<?Header('HTTP/1.1 200 OK');?>
<? require_once("header.php");?>
<? require_once(strtolower($_GET['go'].".php"));?>

    </div>
</div>
<?
    if(!defined("FOOTER"))define('WP_USE_THEMES', false);
    define("FOOTER",true);
    include("blog/index.php");
?>
<!--[if IE]>
<a href='http://www.google.com/chrome' title='Get Google Chrome : A Better Way To Browse' class='noie sprite-1'>IE Sucks</a>
<![endif]-->
<!--[if lt IE 7]>
<div style='position:absolute;top:60px;left:0px;'><b>IE 6</b>?! Dude, upgrade. <br>Click the link above to get Google Chrome.</div>
<![endif]-->
</body>
</html>

在我什至可以发送200之前,有东西正在发送404。

I have the following code in my .htaccess:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

My pretty-link converting is done on my index.php. If the user is looking for something that doesn't exist, a 404 Header is produced, otherwise content is show.

That is all fine and dandy, however when a crawler or something tries to view the HTML, they are getting a 404 error.

For example:

http://www.jasonleodurbin.com/portfolio
That link should work fine.

If you try to validate it at an HTML validator, it doesn't work. The crawler says it is getting a 404. I am getting the same for Facebook share.

I've tried removing the 404 error header, and I am still getting the same problem.

What is the deal? Any suggestions?

EDIT:

New .htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^jasonleodurbin\.com$ [NC]
RewriteRule ^(.*)$ http://www.jasonleodurbin.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+)/$ index.php?go=$1&app=1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+)?success=true index.php?go=$1&success=1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+) index.php?go=$1 [NC,L,QSA]

Processing:

globals.php

if(!isset($_GET['go']) || $_GET['go'] == "") $_GET['go'] = "home";

header.php

if(isset($_GET['go']) && !@fopen(strtolower($_GET['go']).".php",'r')){
    //header("HTTP/1.0 404 Not Found");
    define("FAIL",true);
    $_GET['go'] = "error";
}
else {
    define("FAIL",false);
Header('HTTP/1.1 200');
}
if(FAIL) define("GOSUB",DIR);
if(isset($_GET['app'])) define("GOSUB","../");
else define("GOSUB","");

I've also tried this for the index.php:

<?Header('HTTP/1.1 200 OK');?>
<? require_once("header.php");?>
<? require_once(strtolower($_GET['go'].".php"));?>

    </div>
</div>
<?
    if(!defined("FOOTER"))define('WP_USE_THEMES', false);
    define("FOOTER",true);
    include("blog/index.php");
?>
<!--[if IE]>
<a href='http://www.google.com/chrome' title='Get Google Chrome : A Better Way To Browse' class='noie sprite-1'>IE Sucks</a>
<![endif]-->
<!--[if lt IE 7]>
<div style='position:absolute;top:60px;left:0px;'><b>IE 6</b>?! Dude, upgrade. <br>Click the link above to get Google Chrome.</div>
<![endif]-->
</body>
</html>

Something is sending the 404 before I can even send 200.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甚是思念 2024-09-25 11:15:37

您似乎正在使用 Wordpress。 WordPress 将它找不到的任何页面设置为 404。

此外,您可能对插件 Link Juice 感兴趣守护者。它使用 301 重定向将所有 404 页面重定向到首页。以下是Wordpress 404 提示和技巧

You are seemingly using Wordpress. Wordpress sets any page that it does not find as 404.

Also you may be interested in plugin Link Juice Keeper. It redirects all 404 pages to front page using 301 redirect. And here are Wordpress 404 tips and tricks.

街道布景 2024-09-25 11:15:37

我确实显示了作品集页面,但是带有 404 标头。因此,检查设置 404 标头的代码,错误就在那里。

在我看来,您使用 fopen 只是为了查看文件是否存在(如果打开失败,则它不存在)。使用 file_exists() 来实现:http://php.net/file_exists

I do get the portfolio page show up, but with a 404 header. So, check the code that sets the 404 header, the error is somewhere in there.

Seems to me that you are using fopen only to see if the file exists (if it fails opening, it doesn't exist). Use file_exists() for that: http://php.net/file_exists

俯瞰星空 2024-09-25 11:15:37

您确定代码中没有其他地方可以发送另一个 404 错误吗?如果没有其他人找到,那么这似乎是您的配置问题,请尝试在页面获得批准时发送相反的 200 OK 标头。

顺便说一句,不要执行 fopen(strtolower($_GET['go']) 操作,用户可以将 $_GET['go'] 设置为 http:// /his.domain.com/page,如果 PHP 的配置允许 (allow_url_fopen),您最终会包含来自他的服务器的页面。

Are you sure there's no other place in code where another 404 maybe sent? if no other one find, then it seems to be an issue with your configuration, try sending an opposite 200 OK header when the page is approved.

BTW don't do fopen(strtolower($_GET['go']), the user may set $_GET['go'] to http://his.domain.com/page, and you end up including a page from his server if PHP's configurations allow that (allow_url_fopen).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文