Pretty URLs using Multiviews

October 12, 2006 – 2:03 am

I saw a post on digg.com about how it’s nice to have “pretty urls” that are easy to tell people. While i’m not sure anyone would remember rustyrazorblade.com slash some ridiculous post name, it’s nice to have for search engines. Unfortunately, it didn’t really go into detail about how to accomplish the urls. A lot of people suggested mod_rewrite. Yes, it’s something you can use, but kind of a pain to set up.

A good alternative to mod_rewrite is to use Multiviews. Multiviews are specified in a per-directory basis, and have to be set explicitly.

<Directory /path/to/your/stuff>
Options  Multiviews
</Directory>


You can then use this function to make it useful:

function assign_get_vars()
{
	$args = func_get_args();
	$path_info = $_SERVER['PATH_INFO'];
	$p = explode(”/”, substr($path_info,1) );

	// assign the vars
	$total = count($args);
	for($i=0; $i < $total; $i++)
	{
		$name = $args[$i];
		$_GET[$name] = $p[$i];
		$_REQUEST[$name] = $p[$i];
	}
}


Example:
Say you have this page: /forums/view.php
You can call it like this: /forums/view/444/2

In your PHP script, add this to the top:

assign('thread','page');

You can now refer to $_GET['thread'] and $_GET['page'] as if they were set by the server.

A benefit of Multiviews is that it lets you drop the file extension.

Hope this is of some use to someone. Comments are welcome and encouraged.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  1. 9 Responses to “Pretty URLs using Multiviews”

  2. Nice article. Thanks!

    By Alex on Nov 2, 2006

  3. I’ve been looking everywhere for this! Thank you. Much easier that disecting Wordpress marginalia.

    By Dank Underlord on Jan 19, 2007

  4. Hello,
    I tried to implement this trick, but it doesn’t work. :( Maybe you can help me. :)

    So:

    I have my hosting directory on the server /web/myname (this is my FTP root), and there is a /domain.com directory, my domain name points to this directory. I have a blog.php file, and I want to use the URL /blog (without .php), and as usually I have the /blog/2007/12/25 … etc. format URL.

    How do you think I have to set up with this Multiview? Where should I put the .htacces file, and what is the correct setting?

    Thanks!!

    Kristof

    By Honlap készítés on Feb 17, 2007

  5. You might not have permission to use Multiviews. I believe you would just put it in your .htaccess file under the correct listing. You need to know the exact path to your files for this to work.

    By jon on Feb 17, 2007

  6. Hello!

    I have a problem using Multiviews: it works if I try to display http://dummyurl/index, giving the index.php, but my browser gets confused when I had a slash (…/index/dummypathinfo), and then all relative elements (css, pictures, links, …) get lost or wrong.

    Is there an easy solution to avoid this problem, or should I give always the full path to an element? (this could be annoying to do since there are already many pages that exist and that I would like to use in Multiviews…)

    Thanks!

    BW²

    By BlueWolf² on Mar 21, 2008

  7. You’ll have to do the full path, as the browser is going to pull images / css / whatever relative to the page you’re on, and with multiviews, it can’t figure it out.

    By jon on Mar 22, 2008

  8. You can do a relative path, but set a base href in your header as well. I’m guessing there’s some kind of very slight performance loss, but if you NEED something for previous compatibility (or you might be using something on any number of domains) it might be easier to do it that way.

    By zach on May 14, 2008

  9. You Should try this too!,

    By HBnUV on Jun 22, 2008

  10. Wow.. great.
    I’ll try it.

    I think it’s only can do with .htaccess :)

    By CoLiq on Jun 28, 2008

Post a Comment