Connecting Tech Pros Worldwide Help | Site Map

Pretty URLs

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 4th, 2006, 10:15 PM
Laeronai
Guest
 
Posts: n/a
Default Pretty URLs

I'm making a blog cms and have been having trouble with making the URLs
look good. Each post goes to a URL like "viewpost.php?id=40" but I want
the URL to look like "YYYY/MM/TITLE" so it would come out to be
"/2006/03/hey-look-its-march." Does anyone know how to do this in PHP?

And also, what is this process called? I'd like for the cms to do it
automatically for each post entered.


  #2  
Old March 4th, 2006, 10:25 PM
Carl Vondrick
Guest
 
Posts: n/a
Default Re: Pretty URLs

Laeronai wrote:[color=blue]
> I'm making a blog cms and have been having trouble with making the URLs
> look good. Each post goes to a URL like "viewpost.php?id=40" but I want
> the URL to look like "YYYY/MM/TITLE" so it would come out to be
> "/2006/03/hey-look-its-march." Does anyone know how to do this in PHP?
>
> And also, what is this process called? I'd like for the cms to do it
> automatically for each post entered.
>[/color]

If you are running off Apache, you should look into mod_rewrite. Just
do a Google search for more info.

Good luck

--
Carl Vondrick
www.carlsoft.net
usenet [at] carlsoft [dot] net
  #3  
Old March 5th, 2006, 12:15 AM
Laeronai
Guest
 
Posts: n/a
Default Re: Pretty URLs

Thanks.

  #4  
Old March 5th, 2006, 02:15 AM
Laeronai
Guest
 
Posts: n/a
Default Re: Pretty URLs

Wow that's a complex module....might take a while to understand.

  #5  
Old March 5th, 2006, 07:35 AM
David Quinton
Guest
 
Posts: n/a
Default Re: Pretty URLs

On 4 Mar 2006 19:03:03 -0800, "Laeronai" <wizzlefish@gmail.com> wrote:
[color=blue]
>Wow that's a complex module....might take a while to understand.[/color]

It will do you good!

If you can't / won't use mod_rewrite, I've sometimes used a php
method to achieve the same end result.

Make directory / subdirectories to match the "user friendly" path that
you want people to see / enter.

Place an index.php in the lowest level directory which explodes the
$PATH_INFO into an array.
e.g. list (, $Var1, $Var2) = explode ('/',$PATH_INFO);

Form the "real" url by appending the elements
e.g. $realurl='http://www.mysite.com/myscript.php?firstget=' . $Var1 .
'&amp;secondget=' . $Var2 etc.

Then do a
header("location: $realurl");

You'll also need an .htaccess to
ForceType application/x-httpd-php
DirectoryIndex index.php



Now I've typed all this - you're probably better off with mod_rewrite!
--
Locate your Mobile phone: <http://www.bizorg.co.uk/news.html>
Great gifts: <http://www.ThisBritain.com/ASOS_popup.html>
  #6  
Old March 5th, 2006, 10:25 AM
Janwillem Borleffs
Guest
 
Posts: n/a
Default Re: Pretty URLs

Laeronai wrote:[color=blue]
> I'm making a blog cms and have been having trouble with making the
> URLs look good. Each post goes to a URL like "viewpost.php?id=40" but
> I want the URL to look like "YYYY/MM/TITLE" so it would come out to be
> "/2006/03/hey-look-its-march." Does anyone know how to do this in PHP?
>[/color]

As an alternative to what others have suggested, you could also do the
following:

Say, you want to map http://host/path/33 to http://host/path/?id=33

1. Have the ErrorDocument directive pointing to a file that can handle the
request; let's call it "redirect.php":
ErrorDocument 404 redirect.php

2. In redirect.php, the requested URL can be retrieved from the
$_SERVER['REQUEST_URI'] variable, which can be parsed as follows:

if (preg_match("#^/path/(\d+)/*$#", $_SERVER['REQUEST_URI'], $match)) {
header("Location: http://{$_SERVER['HTTP_HOST']}/path/?id=$match[1]");
exit;
} else {
// Display error message
}


JW



  #7  
Old March 5th, 2006, 02:55 PM
Laeronai
Guest
 
Posts: n/a
Default Re: Pretty URLs

If I used your example, Janwillem, would I include "redirect.php" on
every page? I'm kinda new to PHP, not really sure how to do this kinda
fancy stuff. But I can figure it out.

If I wanted to use /YYYY/MM/title as the format, then I guess I could
retrieve it from the database where the id has to match the one in the
URI, but once I had the date in DATETIME format like YYYY MM how could
I separate the two into two different variables?

Somehow I'd end up with $year and $month, and $title, which would
transform the original title to something all lowercase and hyphens
where all the spaces should be, so "Hello Bob" would become
"hello-bob." Once I had that, I suppose I could use your code.

What does the "#^" mean, and the "(\d+)" mean? I'm a bit confused.

If I were to use mod_rewrite, and I have absolutely no experience in
Apache, how would I use it?

  #8  
Old March 5th, 2006, 03:55 PM
Chung Leong
Guest
 
Posts: n/a
Default Re: Pretty URLs

Hmmm..., but if you redirect to the new URL then it's pretty no longer.
Might make more sense to include the intended script instead.

  #9  
Old March 5th, 2006, 04:55 PM
Janwillem Borleffs
Guest
 
Posts: n/a
Default Re: Pretty URLs

Laeronai wrote:[color=blue]
> If I used your example, Janwillem, would I include "redirect.php" on
> every page? I'm kinda new to PHP, not really sure how to do this kinda
> fancy stuff. But I can figure it out.
>[/color]

The redirect.php page should be part of the ErrorDocument directive shown in
my previous post. In short, when the page doesn't exist, send the request to
redirect.php (ErrorDocument 404 redirect.php)
[color=blue]
> What does the "#^" mean, and the "(\d+)" mean? I'm a bit confused.
>[/color]

This is part of the regular expression synatx. Never heard of it? Start
reading at: http://www.php.net/manual/nl/pcre.pattern.syntax.php
[color=blue]
> If I were to use mod_rewrite, and I have absolutely no experience in
> Apache, how would I use it?
>[/color]

Read about it. It sounds like pretty URL's will be the goal and you will
have to do some learning to reach it. Start to gain some basic knowledge of
regular expressions as you will also need them when applying the mod_rewrite
directives.


JW


  #10  
Old March 6th, 2006, 08:05 AM
Andy Jeffries
Guest
 
Posts: n/a
Default Re: Pretty URLs

On Sat, 04 Mar 2006 15:03:56 -0800, Laeronai wrote:[color=blue]
> I'm making a blog cms and have been having trouble with making the URLs
> look good. Each post goes to a URL like "viewpost.php?id=40" but I want
> the URL to look like "YYYY/MM/TITLE" so it would come out to be
> "/2006/03/hey-look-its-march." Does anyone know how to do this in PHP?[/color]

As another example you could put the following at the top of your
viewpost.php script:

<?php
list($ignore, $year, $month, $title) = explode($_SERVER["PATH_INFO"]);
?>

And link to:

/viewpost.php/2006/03/hey-look-its-march

The server will use viewpost.php as the script and put anything after the
script name that looks like a path into $_SERVER["PATH_INFO"].

Cheers,


Andy


--
Andy Jeffries | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.