Pretty URLs using Multiviews
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.
11 Responses to Pretty URLs using Multiviews
Leave a Reply Cancel reply
Recent Comments
- Anil on MySQL Triggers Tutorial
- Ashish on MySQL Triggers Tutorial
- David on iCal Agenda
- jon on IP address geolocation SQL database
- pim on IP address geolocation SQL database
- jnns on Redis Wildcard Delete
- K.C. Murphy on iCal Agenda
- BA on Experts Exchange should be removed from Google search results
- Andrew on Executing multiple curl requests in parallel with PHP and curl_multi_exec
- Stu on Executing multiple curl requests in parallel with PHP and curl_multi_exec
Recent Posts
- New Project: Jester
- Open New Terminal Tip
- Installing MySQLdb on MacOS Lion
- Headless VM Server Using Ubuntu 11.10
- Get rid of Facebook’s Awful Ticker
- Api Tester now hosted on Github
- Trac .11 jQuery bug
- Multiple Filetypes in Vim
- Git Tip: Setting Up Your Remote Server
- Install issue pymongo on OSX (setuptools out of date)
Categories
- amazon (1)
- answerbag (6)
- apache (9)
- apple (8)
- awk (2)
- bbedit (2)
- c++ (3)
- chrome (2)
- cluster (1)
- cocoa (1)
- collective intelligence (1)
- curl (3)
- db2 (1)
- demand media (1)
- ebay (1)
- eclipse (4)
- erlang (13)
- facebook (1)
- fortran (1)
- gen_server (1)
- git (5)
- google (4)
- haddad (1)
- hdf5 (1)
- html (1)
- innodb (1)
- itunes (1)
- java (2)
- jester (1)
- kvm (1)
- launchbar (1)
- leex (1)
- letsgetnuts.com (1)
- libvirt (1)
- links (6)
- linux (27)
- lucene (1)
- mac (16)
- memcached (1)
- misconception (1)
- mobile (1)
- mono (1)
- mssql (1)
- munin (1)
- mysql (31)
- numpy (1)
- oracle (1)
- php (23)
- puppet (4)
- pyparsing (1)
- pytables (1)
- python (11)
- q&a (1)
- quicksilver (1)
- rant (6)
- readynas (1)
- redis (2)
- regex (1)
- replication (1)
- search (1)
- shitty code (1)
- solr (3)
- spaces (1)
- sshfs (1)
- stored procedure (1)
- svn (5)
- textmate (2)
- tips (22)
- trac (1)
- tutorial (4)
- ubuntu (3)
- Uncategorized (4)
- unix (1)
- vim (3)
- virtual box (6)
- vmware (1)
- weird (3)
- wikipedia (1)
- windows (1)
- xcode (1)








Nice article. Thanks!
I’ve been looking everywhere for this! Thank you. Much easier that disecting WordPress marginalia.
Hello,
Maybe you can help me.
I tried to implement this trick, but it doesn’t work.
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
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.
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²
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.
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.
You Should try this too!,
Wow.. great.
I’ll try it.
I think it’s only can do with .htaccess
hochu vodki!
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.