Page 1 of 2
Emulate single application entry point with .htaccess
Posted: Tue Feb 24, 2009 8:22 pm
by alex.barylski
The project I am working on right now is composed of dozens (30-40) of transaction style scripts.
Each script includes a common global file which does things like authentication, database initialization, etc.
There are quite a few scripts which break SRP and use a conditional test inside the content section to show a different content depending on a 'view' state (the variable name used is probably not used consistently -- could view, page, body, etc).
So we might have a members.php script which handles things like:
- Show listings
- Show profile
- Update listing
All the URI's in the templates are hard-coded (no dynamic route building).
Sounds like the owners now want a SEF setup and I have tried to explain this is a *big* task. Short of editing all the HTML code (some of which could very well be done in functions, others inline in the actual page.
Can you think of any technique I might use to 'just get it to work'? Short of changing all the URI's by hand, even if I could, what exactly would the .htaccess look like when there are 30-40 scripts? Would the .htaccess need to contain a conditional re-write for *every* script? Including the scripts sub-requests (like I show above)?
How would you handle such a situation?
Re: Emulate single application entry point with .htaccess
Posted: Tue Feb 24, 2009 8:31 pm
by Benjamin
Hmm, could you route all requests through a single file which uses a switch to set $_GET variables and then include the appropriate file?
Re: Emulate single application entry point with .htaccess
Posted: Tue Feb 24, 2009 8:46 pm
by alex.barylski
Hmm, could you route all requests through a single file which uses a switch to set $_GET variables and then include the appropriate file?
This is one technique, but what about all the hardcoded URI's? The index.php would be useless (I think) if every link Google seen was in the form:
Re: Emulate single application entry point with .htaccess
Posted: Tue Feb 24, 2009 8:49 pm
by Benjamin
Yeah, you'd have to either search and replace or create a function to rewrite them. You could either do this via output buffering or just search the html for them and replace it with the function.
Re: Emulate single application entry point with .htaccess
Posted: Tue Feb 24, 2009 9:15 pm
by alex.barylski
Output buffering is what I am actually considering. Or perhaps using auto-prepend and re-writing the URI using a massive mapping file -- which will be slow as drool but whatev' -- I didn't build the site so I can't be held responsible.

Re: Emulate single application entry point with .htaccess
Posted: Tue Feb 24, 2009 9:21 pm
by Benjamin
It's not going to be pretty lol.
Re: Emulate single application entry point with .htaccess
Posted: Tue Feb 24, 2009 9:34 pm
by alex.barylski
The solution itself is actually fairly clean, the more I think of it (considering the alternative). The effort required in finding all the possible query strings and re-writing them is an ugly task.
Maybe I can delegate that task to someone else...hmmmm.

Re: Emulate single application entry point with .htaccess
Posted: Wed Feb 25, 2009 6:33 am
by Jenk
ZF does exactly this.
Re: Emulate single application entry point with .htaccess
Posted: Wed Feb 25, 2009 5:44 pm
by alex.barylski
Does exactly what?
These scripts are not controllers but full fledge mini-applicaitons, many of which have several sub-views used under conditional situations.
How would Zend differ from just using a single index.php and including the pages manually then replacing the URI's using a mpping file?
Cheers,
Alex
Re: Emulate single application entry point with .htaccess
Posted: Thu Feb 26, 2009 5:47 am
by Jenk
ZF redirects all requests to a single file, namely index.php in your docroot. All part of their front controller, router and dispatcher implementations.
Re: Emulate single application entry point with .htaccess
Posted: Thu Feb 26, 2009 5:32 pm
by alex.barylski
ZF redirects all requests to a single file, namely index.php in your docroot. All part of their front controller, router and dispatcher implementations.
Haha...OK...but does every PHP CMS known to man kind -- using .htaccess -- how is Zend different?
It's not the routing that so much concerns me, it's the momumental task of re-writing the dynamic URI's and replacing them with static.
Keep in mind that 4-5 other developers have worked on this codebase so one script might have several hardcoded URI's which look like this:
Code: Select all
<a href="help.php?id=5&ppid=27&name=helptip">[Help]</a>
A few lines later another developer adds:
Code: Select all
<a href="help.php?id=5&name=docstip&ppid=2663">[Docs]</a>
Same script similar query string when parsed but in HTML form I would need to find essentially find each URI, parse them and process them accordingly. I wouldn't be surprised if there were GPC variables which were no longer in use, etc...everything is quite a mess...which is why it's daunting -- not exactly a search and replace excersize.

Re: Emulate single application entry point with .htaccess
Posted: Thu Feb 26, 2009 10:27 pm
by allspiritseve
Sounds like you just have to buckle up and FIX IT!!! (SNL, anyone?)
I know Eclipse lets me search for things (including regex if necessary) and it highlights all occurrences in all selected files... so maybe have it select all links, so you don't miss any, and replace them all with clean urls by hand... I can't really suggest any alternative considering you can't search and replace and there's no single thread of logic to the urls.
Re: Emulate single application entry point with .htaccess
Posted: Thu Feb 26, 2009 10:41 pm
by alex.barylski
Sounds like you just have to buckle up and FIX IT!!! (SNL, anyone?)
Saturday Night Live?
It's not so much a matter of buckling up and 'fixing' it. If it were feasible I'd do that. The links cannot be found via regex they are almost entirely constructed on the fly in every location imaginable.
Code: Select all
<a href="<?=$protocol.$domain.$path.'index.php?'."id=$page_id"; ?>"></a>
The same link may also be in the same script file but done like:
Code: Select all
<a href="<?=$protocol.$domain.'somepath/'.$script.'?id'.'='.$page_id;?>"></a>
I can't really suggest any alternative considering you can't search and replace and there's no single thread of logic to the urls
I don't think there is much more to do but:
1. Operate on the resulting HTML before returning to browser
2. Re-write the source from scratch
Re: Emulate single application entry point with .htaccess
Posted: Thu Feb 26, 2009 10:43 pm
by allspiritseve
You can't replace the links by hand? That's what I was suggesting.
Re: Emulate single application entry point with .htaccess
Posted: Thu Feb 26, 2009 10:49 pm
by alex.barylski
You can't replace the links by hand? That's what I was suggesting.
In theory, sure. In reality it would probably take as much as a complete re-write (to get it to 100% perfection anyway).
The owners would probably want this done in less than a week or not at all, which is why I needed a automated (albeit hackish) solution -- such as replacing the URI at run time.