views:

40

answers:

3

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.

+1  A: 

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

robertbasic
Thanks, I changed the file_exists(). Still no fix for the 404.
Jason
+1  A: 

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.

shamittomar
I use wordPress for the /blog sub.
Jason
@Jason, can you post the contents of your index.php file ? If this is your .htaccess file, it should be placed in /blog directory otherwise wordpress will take control of the pages.
shamittomar
I'll edit my original post with my edited .htaccess and the processing I do.
Jason
@Jason - Why are you including the WordPress `index.php` file in your `/index.php` though?
Tim Stone
I am doing this because the wordpress environment is in the blog sub. I include it to get my footer.
Jason
I think you are on to something though, I remove that include and I don't get the header. Is it bad coding practice to resend a 200 OK header?
Jason
@Jason - When you include the WordPress `index.php` file, it automatically tries to route the request path to a WordPress entry (unless you've changed this?). Since you aren't looking for an entry, you just want the footer, it can't find one, and generates a 404 header (after any code where you send a 200 header). You've already generated output though, so it shouldn't be able to do that...unless maybe `output_buffering` is set to 1 in your PHP configuration/you've manually started output buffering?
Tim Stone
I never set any output buffers. I fixed it! I found when WP was sending the header and I shut it off if was being included. Works great now! Thanks for throwing that idea out! It's weird that I wasn't getting a 404 on my localhost. Oh well.
Jason
A: 

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).

aularon
I tried the 200, too. Still nothing.
Jason
Maybe it's not going the 200 route? try `var_dump(FAIL)` to make sure which route the execution is going. p.s. check my answer I edited it adding an important note.
aularon
Where would the weird configuration be?I also use the same idea on the other websites I maintain.http://www.geoffduncanphotography.com/portfolio (for example)The only difference is they aren't in the public_html folder. They are all in separate folders. Could that be an issue?
Jason
Cool. I'll make that change. I assume just do a directory listing and see if my file exists?
Jason
I placed the 200 outside of the if statement which means that it MUST have gone through it. Still nothing.
Jason
the link you posted in your question above is http://www.jasonleodurbin.com/porfolio, it has a typo, it should be http://www.jasonleodurbin.com/portfolio, could the problem be you are checking the invalid link accidentally?
aularon
Good catch. I changed it. But to answer your question, no... that isn't the problem.
Jason
could your file have a BOM utf signature at the first preventing the header from being sent? try surrounding the header call with `ini_set('display_errors', 'on');error_reporting(E_ALL);` befor the call and `ini_set('display_errors', 'off');` after it to see any error that may be hapenning there.
aularon
No errors. Two warnings. But the warnings were not relevant.
Jason
Are you trying it on a local testing server besides that? if yes, is the same problem happening there too?
aularon
How do I check headers on localhost? I was using http://web-sniffer.net/ to check my website.
Jason
use firefox with firebug extension, in firebug's net panel you can see all requests and their headers.
aularon
Nope. I'm not seeing any problem on the localhost.
Jason
I don't know what configuration can cause this, but seems your host have these configurations, can you check with support to trace that? Maybe try to reproduce the problem with just a `.htaccess` and an `index.php` inside a test directory, if it was reproduced given the same scenario here (rewriting, and the server won't send http code header,but working on local host), then it's really a configuration problem.
aularon
I sent an email to my support. Hopefully get a remedy soon. I'll get you updated if we get it fixed.
Jason