NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
SEO friendly URLs in the form of http://HOST/index.php/yada/


Joined: 03 Mar 2007
Posts: 72
Reply with quote
Hello, I've not been around in a while, but I'm hoping Dmitri can assist me with my latest challenge.

I need help with SEO friendly URLs in the form of http://HOST/index.php/yada/yada/yada

PhpDock is returning 404s complaining about not having an alias assigned to deal with the URLs. Here is the long version...


Using Dock with my latest gig. I've coded up a nice little inventory management application using Dojo, CodeIgniter, and MySQL. Going along great. The intent is to deploy as a desktop app, hence PhpDock, and backlink to a web service for enterprise synchronization.

So here is the problem description:

1. CodeIgniter (and me too), loves to use SEO friendly URLs in the form of 'http://HOST/index.php/CONTROLLER/METHOD/VAR1/VAR2/etc'
2. Most of us (CodeIgniter types) use some Apache mod_rewrite .htaccess trickery to replace 'index.php' in our URLs ... then we end up using relative URLs like /CONTROLLER/METHOD/VAR1/VAR2/etc ...
3. In the end, the URI that is finally sent from the browser looks like 'http://HOST/CONTROLLER/METHOD/VAR1/VAR2/etc'
4. MOD_REWRITE correctly makes the URI back into 'http//HOST/index.php/CONTROLLER/METHOD/VAR1/VAR2/etc' and sends it on it's merry way...according to the rules in the .htaccess file

So, as I see it, the mod_rewrite thing is probably out of the question. I can write all my urls in the form of http://host/index.php/yada/yada .

The problem remains that PhpDoc will not allow index.php to process that url since it is (by all proper intents) trying to send it to the last 'yada'.

What can we do?

Thanks for your time,

Randy
View user's profileFind all posts by randySend private messageVisit poster's website
Site Admin

Joined: 13 Jul 2003
Posts: 8328
Reply with quote
although mod_rewrite is supported by Apache servers only, you can try to use phpdock's fail-safe feature in order to get index.php executed.
If you open phpdock.ini, you'll find [ErrorHandlers] and a handler for 404 error in particular. Try to replace it with your version of index.php.

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 03 Mar 2007
Posts: 72
Reply with quote
dmitri wrote:
although mod_rewrite is supported by Apache servers only, you can try to use phpdock's fail-safe feature in order to get index.php executed.
If you open phpdock.ini, you'll find [ErrorHandlers] and a handler for 404 error in particular. Try to replace it with your version of index.php.


Hey dmitri,

Very, very close. This does what you expect.

If you would provide me a very simple variable (such as ${HTTPClient:QueryString}) my problem would be solved.

I believe you could provide this variable simply by pulling ${HTTPClient:InitialUrl} from the ini file and removing that string from the URI requested. Place that into something we can use as ${HTTPClient:QueryString} and I think we have a simple emulation.

I would then able to use the fail safe thus: 404=../web/index.php${HTTPClient:QueryString}

While this is a hack, it is simple for you and solves my problem. Is this an acceptable solution for you?

Thanks for your consideration.

By the way... I'll be leading a PHP training seminar soon and you know I always mention your products. I've not been keeping up with new features on the way. Can you send me an e-mail highlighting some features on the way for your entire line so I can mention these things during my training event?

Thanks,

randy <remove> @randycasburn.com

Randy
View user's profileFind all posts by randySend private messageVisit poster's website


Joined: 14 Jan 2009
Posts: 5
Reply with quote
I need something like this too for use in combination with Zend_Framework. Would it be possible to either integrate mod_rewrite or PATHINFO?
View user's profileFind all posts by TimBroddinSend private message


Joined: 03 Mar 2007
Posts: 72
Reply with quote
TimBroddin wrote:
I need something like this too for use in combination with Zend_Framework. Would it be possible to either integrate mod_rewrite or PATHINFO?


Hi Tim, (think I've seen your name around)

Just my opinion here, but I think it unreasonable for us to ask NuSphere to integrate something apache specific. But I have my reasoning I'll disclose in a sec. We already have separation of the path prior up to index.php since we must provide that in the PhpDock.ini file. The only thing we need is the string, in raw form, after that PhpDock.ini file entry for the ${HTTPClient:InitialUrl}. Then, I'm not certain, but I think we developers can do what we need to emulate platform specific "PATHINFO" like capabilities.

Would you agree that this would satisfy your needs?

Now, here's why I would rather not just throw this over the fence at Dmitri and his small team. I am very interested in seeing a couple key enhancements come from NuSphere. If we have their small team tied up with fringe developments that are not aimed at the core product, it may become a distraction. But this is IMHO. FYI The relevant enhancement for this discussion is a PhpDock for BSD but more specifically for MAC. I need to deploy my applications on MACs just as simply as I do on Windows. I would say Linux as well, but I realize that poses greater challenges and frankly my desktop users are not using Linux. The nerds and geeks and toy makers might be, but the true application users that I am writing business applications for are using Windows and Macs. I need PhpDoc for for both platforms.

Just sayin...

Randy
View user's profileFind all posts by randySend private messageVisit poster's website
Site Admin

Joined: 13 Jul 2003
Posts: 8328
Reply with quote
Quote:
If you would provide me a very simple variable (such as ${HTTPClient:QueryString}) my problem would be solved.

Randy,
info is already provided in three env variables ERROR_FILE, ERROR_DOCUMENT, ERROR_CODE

It's not necessary to be file-not-found error (404). If you have a script that returns 404 or any other http status code >= 400, you can trap and overwrite that with ErrorHandler.

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 03 Mar 2007
Posts: 72
Reply with quote
dmitri wrote:
Quote:
If you would provide me a very simple variable (such as ${HTTPClient:QueryString}) my problem would be solved.

Randy,
info is already provided in three env variables ERROR_FILE, ERROR_DOCUMENT, ERROR_CODE

It's not necessary to be file-not-found error (404). If you have a script that returns 404 or any other http status code >= 400, you can trap and overwrite that with ErrorHandler.


Of course - that's why you are the master Dmitri. Thanks for opening my eyes once again.

I'll provide this script. You and NuSphere are welcome to change, distribute, and do with what you will. It needs some testing as I've not done any. But I think this captures the essence of what you are talking about.

One note - you'll need to add an error handler called something like real404 to the ini. This is simply used by the script as a storage location for the actual 404 error redirection if your framework doesn't handle it for you.

I've been busy packing for a weekend scouting campout with my son. Haven't taken the time to test this.

Tim, perhaps you can use this? Test too?

Randy

Code:
<?php
/**
* A mod_rewrite emulator for PhpDock
*
* @author Randy Casburn
* @version 1.0
* @license BSD -- See Below
* @copyright 2009
* @link http://randycasburn.com
*
* INSTALL: Same directory as PhpDock.ini
*
* In order to make PhpDock compatible with SEO
* friendly URL generators build into many PHP
* frameworks and content managemnt systems (CMS),
* a simple method was necessary to emulate the
* apache mod_rewrite capability that allows
* URLs in the form of:
* http://yourdomain.com/index.php/mycontroller/mymethod/myvar
*
* Several popular PHP Model/View/Controler design
* pattern based frameworks use this structure to
* 'traffic control' their server requests.
*
*----------------------------------------------------
*/

/**
* Your PhpDock file MUST be compliant with the PHP specification
* for ini files.  The most common error is using single quotes
* rather than double quotes to surround text.  Check the docks
* that correspond do parse_ini_file()
*
* @param string file name
* @return array multidimensional array of ini values for PhpDock settings
*/
$aDockSettings = parse_ini_file(dirname(__FILE__).'/PhpDock.ini',true);
/**
* It begins here -- psuedo logic:
* if the initialUrl from the PhpDock.ini file is not the URL
*   then we are looking at an SEO friendly URL
*       send it to out bootstrap controller
* Otherwise
*   this URL is not one we know what to do with
*       that means this is a genuine 404 - let's redirect
*/
if(!strpos($_SERVER['ERROR_DOCUMENT'],strrchr($aDockSettings[HTTPClient][InitialUrl],'/')))
{
    require($aDockSettings[HTTPClient][DefaultDocument]);
}
else
{
    echo $aDockSettings[ErrorHandlers][real404];
}

// Just in case you want to rewrite the URL //

/**
* The workhorse
*
* Simple string substitution with artifacts that already exist
*
* Thanks to Dmitri from NuSphere - Sometimes the most obvious things
* are right in front of your face and you cannot even see them!
*
* @param mixed $aDockSettings
* @return string URI the PhpDock friendly version of the URI
*/
function rewrite_url(& $aDockSettings)
{
    $first_part = substr($aDockSettings[HTTPClient][InitialUrl],0,strripos($aDockSettings[HTTPClient][InitialUrl],':')+1) ;
    $second_part = $aDockSettings[HTTPServer][Listen];
    $initialUrl_part = strrchr($aDockSettings[HTTPClient][InitialUrl],'/');
    $mod_rewritten_part = $_SERVER['ERROR_DOCUMENT'];
    return $first_part.$second_part.$initialUrl_part.'?'.$mod_rewritten_part;
}

/* Copyright (c) 2009, Randy Casburn
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the <organization> nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY <copyright holder> ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

?>
View user's profileFind all posts by randySend private messageVisit poster's website
Site Admin

Joined: 13 Jul 2003
Posts: 8328
Reply with quote
Great work, Randy.
I'm just trying to understand why would you need to store rewrite-settings in phpdock.ini?
If you stored them in your module, you'd save some good time slice spent for ini-parsing.
All values like $aDockSettings[HTTPServer][Listen] can be found in $_SERVER, so ini isn't needed for this. Right?

And honestly, I'd avoid such constructs as $first_part = substr(..,...,strripos(...)+1) ;
What if strripos does not find the subsubstring? A kind of error handling would make your script even better Smile

Finally, I see rewrite_url() function but seems it's not called... Could you please provide a real-world demo with instructions?

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 03 Mar 2007
Posts: 72
Reply with quote
dmitri wrote:
Great work, Randy.
I'm just trying to understand why would you need to store rewrite-settings in phpdock.ini?
If you stored them in your module, you'd save some good time slice spent for ini-parsing.
All values like $aDockSettings[HTTPServer][Listen] can be found in $_SERVER, so ini isn't needed for this. Right?

And honestly, I'd avoid such constructs as $first_part = substr(..,...,strripos(...)+1) ;
What if strripos does not find the subsubstring? A kind of error handling would make your script even better Smile

Finally, I see rewrite_url() function but seems it's not called... Could you please provide a real-world demo with instructions?


Back from the campout in the woods and wanted to address you point very quickly before I can spend any time on this...

'Never called rewrite_url()' - right. I don't need it for my purposes, but I thought Tim (the other poster) or maybe you other clients my find it useful to have a method they could use to rewrite their own URLs in a manner somewhat similar to the Apache does. So I through it together just to be nice Wink

The "DefaultDocument" setting is retrieved from PhpDock.ini -- everything else can be retrieved from $_SERVER. Does PhpDock set the DefaultDocument into $_SERVER? Didn't recognize that. If so, I'll refactor that in.

Next the $first_part, $second_part, yada bits were only left in for instructional purposes. I would ordinarily comment the purpose and they would be gone.

IRT strristr - OK - when I considered this, I my thought was "boy, when this error happens, there are bigger problems than can be solved here, because they have a malformed default URI". But I suppose the least I can do is shoot back the entire string rather than just one character of if it doesn't have any ':'s. I'll change the substr offset parameter to something along the lines of ( (needle === found?) ? needlePos : fullLen ; )
View user's profileFind all posts by randySend private messageVisit poster's website
Site Admin

Joined: 13 Jul 2003
Posts: 8328
Reply with quote
Quote:
when I considered this, I my thought was "boy, when this error happens, there are bigger problems than can be solved here, because they have a malformed default URI".


What's about redirection? For example from http://localhost/ to http://computername/

I think that once it's called as error handler, it should fallback to original error code or better redirecto to pre-defined page supposed to handle the error.

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 20 Jan 2009
Posts: 1
Reply with quote
I am verry much agreed with dmitri's openion.


Last edited by clungtech on Tue Jan 20, 2009 8:02 am; edited 1 time in total
View user's profileFind all posts by clungtechSend private message


Joined: 03 Mar 2007
Posts: 72
Reply with quote
clungtech wrote:
I am verry much agreed with dmitri's openion.



I'll try to get some time to work on this today.
View user's profileFind all posts by randySend private messageVisit poster's website
SEO friendly URLs in the form of http://HOST/index.php/yada/
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 3  

  
  
 Reply to topic