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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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 . '&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> |
|
|||
|
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 |
|
|||
|
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? |
|
|||
|
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 |
|
|||
|
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 |