Bouncing along with mod_rewrite

When we need to redirect users with PHP we can send headers to the browser, indicating where we'd like them to go instead.

<?php header("Location: somewhere_else.php"); ?>

I had a situation however where a client used to have their site hosted on IIS, and in some of their code they had hardlinks to an ASP page that used to be on their old site. I thought about it a bit; one option was to tell Apache to handle pages with ASP extensions with the PHP plugins, which would have worked, but I thought it was a dodgy hack and didn't like the longevity of such a solution.

Instead, I investigated using Apache's mod_rewrite. mod_rewrite is a module that can be plugged into Apache and used to change URLs. I use it on my Gallery installation, so you can go to http://lambie.org/gallery/mobile/ instead of gallery.php?blah=x&y=yoyo&iggy=pop. It can make URLs human-friendly.

What I ended up using the following in a .htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^support_patches\.asp$ index.php?c=21 [R]
</IfModule>

Any calls to support_patches.asp gets redirected to index.php?c=21. The [R] bit means to send the redirect back to the browser, so that the URL actually changes. Without it, we just do an internal redirection, and the user would see the ASP page in their browser's address bar.

5 Comments so far

  1. Aaron on September 28th, 2004

    Welcome to the world of SEO … rewriting is a very popular thing to do for any site that wants decent ratings in google and I just finished converting one of our sites form old skool .asp to .html rewrites all round :P

    Its basically in Beta RC1 at the moment (ie runnign live but obviously with some very very minor bugs) so not going to let anyone know what site yet :P

    But yeah, rewrites are cool and i fyou want your clients to get free traffic, if traffic is important then rewriting URLS is one important thing to do; amongst many others.

  2. Hale on September 29th, 2004

    I used to do the same thing for SEO, except it was millions of html pages (foo.html) being rewritten to a single php page (bar.php?page=foo). Easier to log that way.

    The hoops you have to jump through just to please Google…

  3. Aaron on September 29th, 2004

    Yeah, tell me about it… titles, alt tags with titles of the page you’r linking to blah blah blah :P

    Pays off though, for our .asp site at the moment we are paying quite a bit for traffic but hopefully that shoudl change with .html rewriting. Rewrites give you a lot of power to do liek redirects a lot easier too. If you have a page that was indexed a long time ago and isn’t returning results you can just rewrite to a new page that is etc :)

  4. Hale on September 29th, 2004

    And then after you’ve done all that, and started seeing decent traffic, they do something like the Florida update and it all disappears :)

    Can’t fight 200 PhD’s for very long… they will always win over us normal people.

  5. Aaron on September 30th, 2004

    Unless we kill all humans, sorry PhDs…