Pretty URLs

This is a discussion on Pretty URLs within the PHP Language forums, part of the PHP Programming Forums category; I'm making a blog cms and have been having trouble with making the URLs look good. Each post goes ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-05-2006
Laeronai
 
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.

Reply With Quote
  #2 (permalink)  
Old 03-05-2006
Carl Vondrick
 
Posts: n/a
Default Re: Pretty URLs

Laeronai wrote:
> 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.
>


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
Reply With Quote
  #3 (permalink)  
Old 03-05-2006
Laeronai
 
Posts: n/a
Default Re: Pretty URLs

Thanks.

Reply With Quote
  #4 (permalink)  
Old 03-05-2006
Laeronai
 
Posts: n/a
Default Re: Pretty URLs

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

Reply With Quote
  #5 (permalink)  
Old 03-05-2006
David Quinton
 
Posts: n/a
Default Re: Pretty URLs

On 4 Mar 2006 19:03:03 -0800, "Laeronai" <wizzlefish@gmail.com> wrote:

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


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>
Reply With Quote
  #6 (permalink)  
Old 03-05-2006
Janwillem Borleffs
 
Posts: n/a
Default Re: Pretty URLs

Laeronai wrote:
> 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?
>


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



Reply With Quote
  #7 (permalink)  
Old 03-05-2006
Laeronai
 
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?

Reply With Quote
  #8 (permalink)  
Old 03-05-2006
Chung Leong
 
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.

Reply With Quote
  #9 (permalink)  
Old 03-05-2006
Janwillem Borleffs
 
Posts: n/a
Default Re: Pretty URLs

Laeronai wrote:
> 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.
>


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)

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


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

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


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


Reply With Quote
  #10 (permalink)  
Old 03-06-2006
Andy Jeffries
 
Posts: n/a
Default Re: Pretty URLs

On Sat, 04 Mar 2006 15:03:56 -0800, Laeronai wrote:
> 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?


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

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:19 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0