Emulate single application entry point with .htaccess

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Emulate single application entry point with .htaccess

Post 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

Code: Select all

members.php?view=listings
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?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Emulate single application entry point with .htaccess

Post 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?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Emulate single application entry point with .htaccess

Post 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:

Code: Select all

members.php?view=listings
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Emulate single application entry point with .htaccess

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Emulate single application entry point with .htaccess

Post 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. :D
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Emulate single application entry point with .htaccess

Post by Benjamin »

It's not going to be pretty lol.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Emulate single application entry point with .htaccess

Post 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. :lol:
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Emulate single application entry point with .htaccess

Post by Jenk »

ZF does exactly this.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Emulate single application entry point with .htaccess

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Emulate single application entry point with .htaccess

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Emulate single application entry point with .htaccess

Post 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. :P
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Emulate single application entry point with .htaccess

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Emulate single application entry point with .htaccess

Post by alex.barylski »

Sounds like you just have to buckle up and FIX IT!!! (SNL, anyone?)
Saturday Night Live? :P

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
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Emulate single application entry point with .htaccess

Post by allspiritseve »

You can't replace the links by hand? That's what I was suggesting.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Emulate single application entry point with .htaccess

Post 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.
Post Reply