Status : 404 Not Found

18 posts by 2 authors in: Forums > CMS Builder
Last Post: January 7, 2008   (RSS)

By Dave - January 4, 2008

Ok, instead of:

(contents_of_viewerUrl_field).php//(contents_of_viewerUrl_field)/

Try this (changing filename and removing extra slash):

blog.php/(contents_of_viewerUrl_field)/

The code should generate links like that already. It might have got changed during integration. If you get stuck on it attach your viewer file and I'll have a look. It seems to be working on my end correctly. Let me know how it goes.
Dave Edis - Senior Developer
interactivetools.com

By Dave - January 6, 2008

Absolutely, it's just a matter of figuring out the right PHP code and exactly what you want to do.

If you see if you blog.php code the part that shows the 404:

// error if page not found
if (!$pageRecord) {
header("HTTP/1.1 404 Not Found");
...


That basically says, if we can't find a page, show the 404. We can add a line that checks if there is a / on the end of the url and show the 404 if there is no page -OR- if there is no slash on the end. Like this:

// error if page not found
$urlEndsWithSlash = preg_match("|/$|", @$_SERVER["PATH_INFO"]);
if (!$pageRecord || !$urlEndsWithSlash) {
header("HTTP/1.1 404 Not Found");
...


That will show the 404 for: /blog.php/page2 and also /blog.php since there is no slash on the end. You'd have to use /blog.php/.

If you want a 404 on any page without a slash EXCEPT "/blog.php" then you could use this:

// error if page not found
$urlEndsWithSlash = preg_match("|/$|", @$_SERVER["PATH_INFO"]);
$pageNameSetAndNoSlash = (@$_SERVER["PATH_INFO"] && !$urlEndsWithSlash);
if (!$pageRecord || $pageNameSetAndNoSlash) {
header("HTTP/1.1 404 Not Found");
...


The PHP preg_match() function used "regular expressions" to see if something matches a pattern. In this case, to see if the url ends in a slash. Anyway, hope that helps! :)
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Status : 404 Not Found

By Djulia - January 6, 2008 - edited: January 6, 2008

Hi Dave,

Thank you, your code works marvelously. [:)]

I also noticed that blogPage or blogPage.php gives the same result.

/folder/blogPage[/#0080ff]/page1/
/folder/blogPage[/#0080ff].php[/#ff0000]/page1/

You think, that it is possible to obtain a result only for .../blogPage/...[/#0080ff] ?

Then, .../blogPage.php/... would give an error 404 ?

Thank you for your answer.

Julia

By Dave - January 6, 2008

You must have something setup on your server so "blogPage" works without the ".php". Usually it wouldn't without some extra changes.

Anyway, the script url is stored in $_SERVER["PHP_SELF"] so you can use the preg_match() function to check if there's a .php in the url.

$hasDotPHP = preg_match("/\.php/", $_SERVER['PHP_SELF']);

And then add that as "OR $hasDotPHP" to your 404 check:

$hasDotPHP = preg_match("/\.php/", $_SERVER['PHP_SELF']);
if (!$pageRecord || $pageNameSetAndNoSlash || $hasDotPHP) {

Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Status : 404 Not Found

By Djulia - January 6, 2008

Hi Dave,

But, you think, that one can use the two codes together ?

$urlEndsWithSlash = preg_match("|/$|", @$_SERVER["PATH_INFO"]);
if (!$pageRecord || !$urlEndsWithSlash) {


$hasDotPHP = preg_match("/\.php/", $_SERVER['PHP_SELF']);
if (!$pageRecord || $pageNameSetAndNoSlash || $hasDotPHP) {




I made several tests, but I do not manage to obtain a satisfactory result.

The idea, it is to obtain an error 404 for : /folder/blogPage.php[/#ff0000]/page/

and not for : /folder/blogPage/page/

But : /folder/blogPage/page
must also give an error 404.

Thank,

Julia

By Dave - January 6, 2008

I wasn't sure which code you were using, you just need to add "|| $hasDotPHP" to the end of the if() statement. If it's not working I'm not sure why but you could track it down by printing out some values like this:

PHP_SELF = '<?php echo $_SERVER['PHP_SELF']; ?>'
hasDotPHP = '<?php echo $hasDotPHP; ?>'


PHP_SELF should have the script url "/folder/blogPage.php/" and if it has ".php" in it then it should set $hasDotPHP. Then if you know it has ".php" you could display the 404. Checking for the .php shouldn't change how anything else is working.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Status : 404 Not Found

By Djulia - January 7, 2008

Thank you for your assistance.

Your suggestion enabled me to correct my problem.

In fact, my server does not guarantee the use of : ( $_SERVER['PHP_SELF'] ).

In my case, it gives "/page1/" for "/folder/blogPage.php/page1/"

But, it seems that the use of ( $_SERVER['SCRIPT_NAME'] ) also works.

Thank you, you are a very good teacher.

Thank,

Julia