mod_rewrite before of after developing application - php

I am trying to better understand how to incorporate mod_rewrite into a web applications development life cycle.
The question I am trying to understand is:
Do you build your application and then setup mod_rewrite after the fact?
Is it more appropriate to consider mod_rewrite along the way while you develop your application?

mod_rewrite is a substantial part of your application. If it doesn't handle just trivial requests, but for example establishes a front controller, then of course you have to have planned your application structure to know what mod_rewrite is supposed to do.

Whenever I'm building an application - first thing what I do is to redirect any /foo/bar/ or /foo/bar.html into a PHP index.php?path=foo/bar. More detailed setup ( for example for images etc. ) I do when I need it

Related

How to create a proper routing system for a no-framework PHP web application?

I am a beginner in PHP development, and I have recently been asked to create a small web application from scratch. I did some research and I decided that I DO NOT want to use any framework, because I truly want to learn how to create decent code myself.
The thing is, I am creating a basic MVC structure to generate a basic CRUD for a model that I have, but I am totally clueless about the best practices to get clean, useful URLs for my application.
I am running on Ubuntu 16.04 LTS, using Apache as a webserver with PHP 5.6 and MySQL. My first idea was to redirect all the requests through index.php and arrange some sort of mechanism in an .htaccess file to rewrite ugly URLs into more friendly and simpler ones, but to be honest, I do not even know where to begin with.
How am I supposed to handle the requests in my index.php file to redirect the user to the proper Controller and do the required action in there? Am I taking the right path regarding this?

Replicate Google App Engine app.yaml handlers in PHP outside of GAE

I am considering building my PHP project in Google App Engine (GAE), and I would like to be confident that I could easily port my code to a more standard Apache Tomcat & PHP server if needed. The one aspect that I cannot find a standard implementation for is GAE's app.yaml handlers. Let me define the functionality that I care about:
The ability to explicitly route incoming requests to a specific php script for fulfillment based on URL patterns.
My searches thus far have lead me to the Tomcat .htaccess RewriteEngine. However, it seems like this literally re-writes the URL and redirects the client machine to that URL. Am I wrong? Other than this solution, I have found nothing else that is promising. Can someone suggest a replacement for GAE's app.yaml handlers?
Thanks in advance!
Seems like you are looking for Apache mod_rewrite which lets you setup rewrite rules that are interpreted behinds the scenes and do not change the external URL.

Integrating Zend Framework and WordPress, Delegating page loads

I'm looking to have a full integration between Zend Framework and WordPress. For clarification, this does not mean I want to use Zend Libraries within WordPress, but rather delegating the page loads for a single site between both systems.
I've done a fair bit of research, I've come across several items, but nothing quite like what I'm looking for. I want to be able to load a page from the MVC if it exists and is available, otherwise load from WordPress or vice versa.
Now, I think I could approach this in a manner of combining the index files of WordPress and Zend Framework. Doing so, I would need to have a type of "check" against the incoming requests to decide which system to hit. I would suppose I would have a something in cache, containing all the Wordpress uri's to check against (that would be updated through a cron job / daemon), if not, serve up the bootstrap from Zend Framework.
Any suggestions would be greatly appreciated.
If all you're trying to do is have a Wordpress blog on a ZF site, then just put the blog on a subdomain or use mod_rewrite to rewrite all requests to blog/ to Wordpress.
If you're trying to do something more complicated that requires interaction between the two systems, I would suggest you route all requests to ZF (using the standard rewrite rules). Then let ZF decide whether or not it can handle the request, and if not, include the Wordpress index.php and let Wordpress do it's thing. See the answer here for some more detail: Converting a Brownfield PHP Webapp to Zend Framework.
You want to avoid making any changes to Wordpress itself if at all possible, as otherwise upgrading WP will become a painful process.
One thing I can imagine is that you pretest any request. So you send a sub-request with apache to wordpress, and if it's status 404 you delegate the request to zend.
All non 404 requests then need to be delegated (again) to wordpress.
But handle with care. I don't think this is really what you want.

Routing page requests with PHP the right way

I need to know the term and best practices of performing site navigation the "right"? way, similar to how stackoverflow routes you when you ask a question via the url:
"http://stackoverflow.com/questions/ask"
Where as with my knowledge of PHP programming I would probably code it like so:
"http://stackoverflow.com/index.php?p=questions&act=ask"
Hopefully you understand what I mean. I would like to know the term for this method of page navigation and request/response handling, and if possible the best practices, limitations, or anything else I need to keep in mind when designing a web application using this standard / method. I also don't even know if this is all done with PHP or some web backend coded in ASP or Ruby or what have you, so I have populated the tags with my guesses.
The pattern that most MVC frameworks use is a front controller that calls on a router. The front controller is typically an index.php in your web root. Next, all requests that aren't for existing files (like js, css, and image assets) need to be sent to this controller. In apache, you can do this with mod_rewrite:
RewriteRule ^index\.php$ - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L,QSA]
However, the recommended way in Apache 2.5 is with FallbackResource:
FallbackResource index.php
IIS has similar functionality if that's what you're using.
In index.php, you can access the URL originally requested with $_SERVER['REQUEST_URI']. You should include your router (which should be outside of the web root) and invoke it with the request URI. Example:
require '../router.php';
$router = new Router();
$router->process($_SERVER['HTTP_METHOD'], $_SERVER['REQUEST_URI'], $_GET, $_POST);
Then your router can find the appropriate controller to route the request to. Read more on the MVC framework, and study some examples to better understand how others have implemented it.
They are most likely using the same method you are describing (embedding the navigation variables) within the URL, but it is being done "under the hood".
The mechanism that allows you to present URLs such as this is called MOD Rewrite. It uses the combination of the variables in the URL, and regular expressions to re-represent the URL to the end-user in a more user-friendly manner.
More Information: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Edit: Of course this would apply to code running on apache webserver. There are probably similar modules for other web servers such as IIS.
Also, mind you that mod_rewrite is outside of the scope of php. It is instead actually apache server directives, which are invoked before php even comes into play.
I am currently developing a php router which is targeted at extreme high performance. you probably might want to take a look:
https://github.com/c9s/Pux
Pux is 48.5x faster than symfony router in static route dispatching, 31x faster in regular expression dispatching. (with pux extension installed)
Pux tries not to consume computation time to build all routes dynamically (like Symfony/Routing). Instead, Pux compiles your routes to plain PHP array for caching, the compiled routes can be loaded from cache very fast.
With Pux PHP Extension support, you may load and dispatch the routes 1.5~2x faster than pure PHP Pux.
Introducing RouteREST
https://github.com/iPhoneSDKPro/routeREST
This is my implementation of a php router. It uses MVC architecture and is designed to be simple to setup and use.
I use this in a project I developed and thought to share with the community.
At some point I would like to make it into a "Composer" library.. someday..
Enjoy
-James

URL re-writing in PHP

I am trying to implement URL rewriting in my PHP application. Can someone share a step by step procedure of implementing URL rewriting in PHP
In my application I want to implement following URL rewriting
http://example.com/fast-five
http://example.com/300
http://example.com/13-b
from
http://example.com/movie-download.php?nm=fast-five
http://example.com/movie-download.php?nm=300
http://example.com/movie-download.php?nm=13-b
One more thing which URL will be best according to SEO, management, application point-of-view out of the following two types.
Of course
http://example.com/fast-five
will be good for SEO
Are you serving your PHP through an Apache HTTP Server installation? If so:
RewriteRule ^/fast-five$ /movie-download.php?nm=fast-five [R=301]
From an SEO perspective, the first would be preferred. Using the HTTP 301 ("Moved Permanently") is most effective for this.
If your using an MVC framework like CakePHP, you should look at the documentation on routing. Otherwise, you can use the web servers rewriting rules.

Categories