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
 

11 Responses to Pretty URLs using Multiviews

  1. Alex says:

    Nice article. Thanks!

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

  3. 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

  4. jon says:

    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.

  5. BlueWolf² says:

    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²

  6. jon says:

    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.

  7. zach says:

    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.

  8. HBnUV says:

    You Should try this too!,

  9. CoLiq says:

    Wow.. great.
    I’ll try it.

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

  10. Andrew Moyer says:

    You may also need to include the following in your Apache httpd.conf or .htaccess file if allowed:

    AddHandler php5-script php
    AddType text/html php

    That’s what finally got it working for me with an extension in the file name but not in the URL (in addition to using MultiViews). This goes for Apache 2.2. I believe the original post on this page would have worked in the 1.3 branch of Apache… but the 2.x branch needs more.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>