Search code examples
phpmod-rewritehttp-redirectcanonical-link

Handling site-wide URL rewrites with PHP


I'm looking into the feasibility of using PHP - instead of mod_rewrite - to handle URL canonicalization. I'm looking to be able to map a large number of different URLs to a given physical PHP page, and handle 301's and 404's in a more centralized and maintainable way. This will include common misspellings, aliases, search engine friendly URL parameters, and the like. These needs seem well outside the power of mod_rewrite, so I'm looking into other options.

I'm thinking I would create a canonical.php script which I map every page to with the following in .htaccess (borrowed from this post):

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ canonical.php/$1?%{QUERY_STRING} [L]

And then canonical.php would do whatever URL parsing / db lookups / redirects / etc. are necessary, then simply include /the/appropriate/file.php for the given request.

Is this a reasonable course of action? Is such functionality actually feasible with mod_rewrite directly? (DB lookups and the like aside) will this be distinctly slower than mod_rewrite? Is there any other methodology that's more robust than a PHP wrapper?


Solution

  • You're talking about routing, which plenty of frameworks do. Take a look at this answer: https://stackoverflow.com/questions/115629/simplest-php-routing-framework