1

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?

Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
dimo414
  • 47,227
  • 18
  • 148
  • 244

3 Answers3

1

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

Community
  • 1
  • 1
Dan
  • 379
  • 2
  • 9
0

What I would suggest and what I use is a php file that gets called everytime when a 404 is encountered and it stores the url encountered. Then once a week I go to the management console and I map the wrongly spelled mistyped, old urls, searchengine's history url's to the current existing urls then I hit the parse button and it spews out a new updated .htaccess file out for me.

That way you lighten the load on your database, loading time, compiling time and redirecting time.

Just my 2 cents.

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • Good idea, I'm looking for more than that, but auto-generating an .htaccess file is not a bad plan. I wonder if there's a point where the number of lines in .htaccess could introduce a greater slowdown than a more dynamic solution. – dimo414 Jun 18 '12 at 19:04
  • There are people who have 30k+ line's of anti bot, hack, spam processed via .htaccess and they have no problems concerning speed. – Tschallacka Jun 18 '12 at 19:05
0

Haven't had a chance to test this, but this answer pointed me at Apache's FallbackResource directive, which sounds very promising.

Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244