If we have many rules which cannot be normalized, there would be a long list of rules in .htaccess, which would probably behave as a long if-else condition.
Is there any way, we can apply rewritting rules effeciently for long list of rules?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, not using .htaccess at all. Just re-route everything (except images, js, css, etc.) to index.php and handle routing business in PHP, not in .htaccess.
Using .htaccess is a pain. Just forward everything to index.php and go from there. If the url is http://www.google.com/test/bla/bla/, it gets rerouted to http://www.google.com/index.php/test/bla/bla/. In index.php you can then explode the string ‘test/bla/bla/’ to get your arguments in an array.
You can do whatever you want from there.
EDIT:
There’s no real overhead in this solution. You just create infinite flexibility. You’ll just need a few things:
A) A url parser. You can make this as complicated as you want. You can have it explode the url and that’s it. You can extend it with complex functionality like Reflection, etc. A minimalist url parser contains maybe 5 lines of code. (Get the url, explode, return the array.)
B) Some sort of router/routing mechanism. Determine ‘where’ you should send the request based on the url. The url parser gave you a list of arguments – what is the structure of that array?
Is the first argument always some sort of controller? Then try finding that controller. Is the second argument always an action? Search for the corresponding action within that controller.
Or… does the first argument always resolve to a specific page? Then include that specific page.
It all depends on your requirements and your application, but one thing is for sure: it’s always easier to work with than with .htaccess. You can implement this basically in any stage of your application development, as the url’s stay the same, but only the handling differs.
EDIT2
Oh, and something else – if you implement this solution and you end up with either a switch statement with 100 cases or an endless if/else condition block: you’re doing it wrong. You should always be able to have some sort of automated routing.
If your application works with procedural code and you use includes: put all the ‘includeable’ pages in a folder and just try to resolve the page by including it. If it’s non-existant, throw an error. No need to put every single page in a switch statement.
On the specific pages themselves; check whether the user has enough rights to view that page. Don’t do that whilst routing, the router shouldn’t be aware of security context.
If you have an object oriented structure with controllers, make sure you can identify ‘action methods’. Use docblocks, prefixes (doSomething()) or whatever to make sure the router can only access methods that are supposed to be accessed from the outside. As for security: don’t validate whether the user is allowed to call that action in the router. In an object oriented application there are 100 styles of access control management, but the router is just for routing. Add a security layer of some sort, don’t let the router decide whether the user can come in. You can reroute the request if some security mechanism finds out the user doesn’t have enough credentials. Reroute to some ‘illegal request’ page.
This answer got a bit too long and elaborates more on other topics than just on what you asked for, for which I’m sorry. 🙂