Connecting Tech Pros Worldwide Forums | Help | Site Map

Pretty URLs

Laeronai
Guest
 
Posts: n/a
#1: Mar 4 '06
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.


Carl Vondrick
Guest
 
Posts: n/a
#2: Mar 4 '06

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
Laeronai
Guest
 
Posts: n/a
#3: Mar 5 '06

re: Pretty URLs


Thanks.

Laeronai
Guest
 
Posts: n/a
#4: Mar 5 '06

re: Pretty URLs


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

David Quinton
Guest
 
Posts: n/a
#5: Mar 5 '06

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>
Janwillem Borleffs
Guest
 
Posts: n/a
#6: Mar 5 '06

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



Laeronai
Guest
 
Posts: n/a
#7: Mar 5 '06

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?

Chung Leong
Guest
 
Posts: n/a
#8: Mar 5 '06

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.

Janwillem Borleffs
Guest
 
Posts: n/a
#9: Mar 5 '06

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


Andy Jeffries
Guest
 
Posts: n/a
#10: Mar 6 '06

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

Closed Thread